diff --git a/mlir/include/mlir/Conversion/SolToYul/EVMUtil.h b/mlir/include/mlir/Conversion/SolToYul/EVMUtil.h index 2c14124cc4cd..f33f33f1c71b 100644 --- a/mlir/include/mlir/Conversion/SolToYul/EVMUtil.h +++ b/mlir/include/mlir/Conversion/SolToYul/EVMUtil.h @@ -22,8 +22,6 @@ #include namespace mlir { -class TypeConverter; - namespace evm { class StringLiteralPool; @@ -38,14 +36,19 @@ enum ByteLen { using AllocSize = int64_t; +/// Returns the type a value of `ty` has in Yul. +Type getYulType(Type ty); + /// Returns the alignment of the LLVMPointerType value `ptr` unsigned getAlignment(Value ptr); -/// Returns true when \p v is a compile-time constant. The type converter may -/// leave a single materialization cast over the constant, which is looked -/// through. +/// Returns true when \p v is a compile-time constant. bool isConstantValue(Value v); +/// Creates the private function `name` of type `ty`. +yul::FuncOp createPrivateFunc(OpBuilder &b, Location loc, StringRef name, + FunctionType ty); + // FIXME: Remove this! The lowering should not expand the ABI encoding/decoding // code (where this is used) involved in sol.contract, sol.emit etc. lowering. // Instead it should generate a high level sol op that can have a custom @@ -128,9 +131,6 @@ class Builder { this->literals = &literals; } - // - // The following APIs are used in the stage1 lowering. - // // TODO? Some of these could be transformed to mlir ops instead. This would // allow targets to define custom lowering. For instance, some targets might // not need the `type(uint64).max` check in the memory allocation code @@ -389,11 +389,6 @@ class Builder { bool fromMem, Location loc); public: - // - // TODO? Should we work with the high level types + OpAdaptor for the APIs - // that work with low level integral type pointers? - // - /// Generates a low level integral type pointer to the address holding the /// data of a dynamic allocation. Value genDataAddrPtr(Value addr, Type ty, @@ -696,11 +691,9 @@ class Builder { bool topLevel, std::optional locArg = std::nullopt); /// Generates the tuple decoder code as per the ABI and populates the results. - /// A non-empty tuple is decoded by `helpersym::abiDecodeTuple`, whose - /// results are the types \p tyConv gives \p tys. + /// A non-empty tuple is decoded by `helpersym::abiDecodeTuple`. void genABITupleDecoding(TypeRange tys, Value tupleStart, Value tupleEnd, std::vector &results, bool fromMem, - const TypeConverter &tyConv, std::optional locArg = std::nullopt); /// Generates a panic with \p code through `helpersym::panic`. void genPanic(PanicCode code, std::optional locArg = std::nullopt); diff --git a/mlir/include/mlir/Conversion/SolToYul/SolToYul.h b/mlir/include/mlir/Conversion/SolToYul/SolToYul.h index ae2dc46060d2..74b5639c6e9a 100644 --- a/mlir/include/mlir/Conversion/SolToYul/SolToYul.h +++ b/mlir/include/mlir/Conversion/SolToYul/SolToYul.h @@ -1,4 +1,4 @@ -//===- SolToYul.h - Sol to Yul dialect conversion ---------------*- C++ -*-===// +//===- SolToYul.h - Sol to Yul lowering -------------------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -6,8 +6,8 @@ // //===----------------------------------------------------------------------===// // -// This file declares the Sol-to-Yul conversion pass and its EVM-specific -// lowering patterns. +// This file declares the Sol to Yul lowering pass and the state it shares with +// the EVM utilities. // //===----------------------------------------------------------------------===// @@ -17,9 +17,7 @@ #include "mlir/Dialect/LLVMIR/LLVMDialect.h" #include "mlir/Dialect/Sol/Sol.h" #include "mlir/Dialect/Yul/Yul.h" -#include "mlir/IR/PatternMatch.h" #include "mlir/Pass/Pass.h" -#include "mlir/Transforms/DialectConversion.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/MapVector.h" #include "llvm/ADT/SetVector.h" @@ -32,40 +30,25 @@ namespace mlir { namespace evm { -class SolTypeConverter : public TypeConverter { -public: - SolTypeConverter(); -}; - -/// The internal function pointer dispatch of a module. Inline assembly can +/// The internal function pointer dispatch of a contract. Inline assembly can /// retype a pointer, so a call reaches every target that takes and returns as -/// many words. The pass records its contract's creation_func_refs before -/// stage 2 erases the contract. Stage 1 records each sol.func_constant's target -/// and turns each sol.icall into a call of its dispatch function, one per -/// lowered signature, declared at its first call. The pass gives the dispatch -/// functions their bodies after stage 2 has moved the functions into their -/// object. +/// many words. Each call goes through the dispatch function of its signature, +/// declared at its first call; the bodies are generated once every target is +/// known. class InternalDispatch { public: /// The words a function takes and returns. using WordCounts = std::pair; - explicit InternalDispatch(const TypeConverter &tyConv) : tyConv(tyConv) {} + /// Records `fn`, of pointer value `id`, as a target of the calls that take + /// and return as many words as it does. + void addTarget(yul::FuncOp fn, int64_t id); - /// Records the functions `contr` lists in its creation_func_refs. - void addCreationFuncRefs(sol::ContractOp contr); - - /// Records `fn` as a target of the calls that take and return as many words - /// as it does. - void addTarget(sol::FuncOp fn); - - /// Returns the dispatch function `icall` calls, of `dispatchFnTy`: the - /// pointer ahead of the callee's arguments, to the callee's results. The - /// first call of that signature declares it, so every call names an existing - /// function. - yul::FuncOp getOrCreateDispatchFn(sol::ICallOp icall, - FunctionType dispatchFnTy, - PatternRewriter &r); + /// Returns the dispatch function of `dispatchFnTy`: the pointer ahead of the + /// callee's arguments, to the callee's results. The first call, from + /// `caller`, declares it. + yul::FuncOp getOrCreateDispatchFn(FunctionType dispatchFnTy, + yul::FuncOp caller, OpBuilder &b); /// Gives each declared dispatch function its body, a switch over the targets /// of its word counts, and places it after its first caller. @@ -74,13 +57,14 @@ class InternalDispatch { private: struct Request { yul::FuncOp fn; - sol::FuncOp caller; + yul::FuncOp caller; }; - const TypeConverter &tyConv; /// The targets of the calls of each word counts, in the order they are /// recorded, which the cases of a dispatch follow. - DenseMap> targets; + DenseMap> targets; + /// The pointer value of each target. + DenseMap ids; llvm::MapVector requests; }; @@ -98,78 +82,6 @@ class StringLiteralPool { llvm::StringMap globals; }; -/// Adds the conversion patterns of unchecked arithmetic ops in the sol dialect. -void populateArithPats(RewritePatternSet &pats, TypeConverter &tyConv); - -/// Adds the conversion patterns of internal function pointers: the constants -/// and the calls through them, which `dispatch` records. -void populateInternalFuncPtrPats(RewritePatternSet &pats, TypeConverter &tyConv, - InternalDispatch &dispatch); - -/// Adds the conversion patterns of crypto ops in the sol dialect. Literal -/// inputs take their globals from `literals`. -void populateCryptoPats(RewritePatternSet &pats, TypeConverter &tyConv, - StringLiteralPool &literals); - -/// Adds the conversion patterns of checked arithmetic ops in the sol dialect. -void populateCheckedArithPats(RewritePatternSet &pats, TypeConverter &tyConv); - -/// Adds the conversion patterns of sol dialect ops dealing with stack, memory -/// and storage allocations. String literals and literal arguments of -/// sol.concat take their globals from `literals`. -/// -/// Stack access will have 32-byte alignment while all other access will have -/// 1-byte alignment. -void populateMemPats(RewritePatternSet &pats, TypeConverter &tyConv, - StringLiteralPool &literals); - -/// Adds the conversion pattern of sol.emit. Literal arguments take their -/// globals from `literals`. -void populateEmitPat(RewritePatternSet &pats, TypeConverter &tyConv, - StringLiteralPool &literals); - -/// Adds the pattern that erases a sol.constant_string, which its consumers -/// leave unused once they are lowered. -void populateConstantStringPat(RewritePatternSet &pats); - -/// Adds the conversion pattern of sol.require. Literal reasons take their -/// globals from `literals`. -void populateRequirePat(RewritePatternSet &pats, StringLiteralPool &literals); - -/// Adds the conversion patterns for address/block/tx/msg/gas-related ops. -void populateAddrPat(RewritePatternSet &pats, TypeConverter &tyConv); - -/// Adds the conversion patterns of abi ops. Encoded literals take their -/// globals from `literals`. -void populateAbiPats(RewritePatternSet &pats, TypeConverter &tyConv, - StringLiteralPool &literals); - -/// Adds the conversion pattern of sol.ext_call. Literal arguments and inputs -/// take their globals from `literals`. -void populateExtCallPat(RewritePatternSet &pats, TypeConverter &tyConv, - StringLiteralPool &literals); - -/// Adds the conversion patterns of control flow ops in the sol dialect. -void populateControlFlowPats(RewritePatternSet &pats, TypeConverter &tyConv); - -/// Adds the conversion patterns of sol.call and sol.return. -void populateFuncBoundaryPats(RewritePatternSet &pats, TypeConverter &tyConv); - -/// Adds the conversion pattern of sol.func. -void populateFuncOpPats(RewritePatternSet &pats, TypeConverter &tyConv); - -/// Adds the conversion pattern of sol.contract. -void populateContractPat(RewritePatternSet &pats, const TypeConverter &tyConv); - -/// Adds patterns for sol.inline_asm and related ops. -void populateInlineAsmPats(RewritePatternSet &pats, TypeConverter &tyConv); - -void populateStage1Pats(RewritePatternSet &pats, TypeConverter &tyConv, - InternalDispatch &dispatch, - StringLiteralPool &literals); - -void populateStage2Pats(RewritePatternSet &pats, const TypeConverter &tyConv); - } // namespace evm } // namespace mlir diff --git a/mlir/include/mlir/Conversion/SolToYul/Util.h b/mlir/include/mlir/Conversion/SolToYul/Util.h index 3a22d85c4e5d..b97eedfd3691 100644 --- a/mlir/include/mlir/Conversion/SolToYul/Util.h +++ b/mlir/include/mlir/Conversion/SolToYul/Util.h @@ -83,6 +83,14 @@ class BuilderExt { return genConst(llvm::APInt(256, val, /*isSigned=*/true), locArg); } + /// Generates `val`, a value of `ty`, extended to 256 bits by the signedness + /// of `ty`. + Value genI256Const(llvm::APInt const &val, IntegerType ty, + std::optional locArg = std::nullopt) { + assert(val.getBitWidth() <= 256 && "Expected constant width <= 256"); + return genConst(ty.isSigned() ? val.sext(256) : val.zext(256), locArg); + } + /// Generates an i256 constant with the selector in the high 32 bits. Value genI256Selector(StringRef signature, std::optional locArg = std::nullopt) { diff --git a/mlir/include/mlir/Dialect/Sol/SolOps.td b/mlir/include/mlir/Dialect/Sol/SolOps.td index 70adbb5cc019..4a2b487acd08 100644 --- a/mlir/include/mlir/Dialect/Sol/SolOps.td +++ b/mlir/include/mlir/Dialect/Sol/SolOps.td @@ -27,19 +27,6 @@ include "mlir/IR/SymbolInterfaces.td" // specific ops) should verify that their types (ssa argument types and // TypeAttr) are high level ((un)signed int types, sol dialect types etc.). -// FIXME: Remove this! We're already running reconcile-unrealized-casts, so we -// should start using builtin.unrealized_conversion_cast -def Sol_ConvCastOp : Sol_Op<"conv_cast", [Pure]> { - let summary = "No-op cast that casts to the remapped value in conversion"; - - let arguments = (ins AnyType:$inp); - let results = (outs AnyType:$out); - - let assemblyFormat = "$inp attr-dict `:` type($inp) `->` type($out)"; - - let hasFolder = 1; -} - def Sol_ConstantOp : Op, diff --git a/mlir/lib/Conversion/SolToYul/CMakeLists.txt b/mlir/lib/Conversion/SolToYul/CMakeLists.txt index 1ee98ec93100..260349b5b9a6 100644 --- a/mlir/lib/Conversion/SolToYul/CMakeLists.txt +++ b/mlir/lib/Conversion/SolToYul/CMakeLists.txt @@ -3,8 +3,6 @@ add_mlir_conversion_library(MLIRSolToYul HelperSymbols.cpp InternalDispatch.cpp SolToYul.cpp - SolToYulPass.cpp - TypeConverter.cpp Util.cpp ADDITIONAL_HEADER_DIRS @@ -18,8 +16,6 @@ add_mlir_conversion_library(MLIRSolToYul MLIRYulDialect MLIRLLVMDialect MLIRPass - MLIRTransforms - MLIRTransformUtils LINK_COMPONENTS Support diff --git a/mlir/lib/Conversion/SolToYul/EVMUtil.cpp b/mlir/lib/Conversion/SolToYul/EVMUtil.cpp index 6f639a1b52a8..15c1dc1467f5 100644 --- a/mlir/lib/Conversion/SolToYul/EVMUtil.cpp +++ b/mlir/lib/Conversion/SolToYul/EVMUtil.cpp @@ -23,9 +23,9 @@ #include "mlir/Dialect/Sol/Sol.h" #include "mlir/Dialect/Yul/Yul.h" #include "mlir/IR/Matchers.h" -#include "mlir/Transforms/DialectConversion.h" #include "llvm/ADT/DenseSet.h" #include "llvm/ADT/StringExtras.h" +#include "llvm/ADT/TypeSwitch.h" #include "llvm/Support/raw_ostream.h" #include #include @@ -889,6 +889,82 @@ void evm::Builder::genBulkWordCopy(Value dstAddr, Value srcAddr, Value count, srcDataLoc, loc); } +Type evm::getYulType(Type ty) { + MLIRContext *ctx = ty.getContext(); + Type i256Ty = IntegerType::get(ctx, 256); + // A {word, word} pair: the {addr, size} of a calldata slice, the {slot, + // byte offset} of a packed storage value, or the {addr, selector} of an + // external function, each LSB-aligned in its word. + Type pairTy = LLVM::LLVMStructType::getLiteral(ctx, {i256Ty, i256Ty}); + + return TypeSwitch(ty) + // Signedness and width live on in the Sol types the lowering reads. + .Case( + [&](Type) { return i256Ty; }) + // The function's id in the internal dispatch. + .Case([&](sol::FuncRefType) { return i256Ty; }) + .Case([&](sol::ExtFuncRefType) { return pairTy; }) + .Case([&](FunctionType fnTy) { + return FunctionType::get( + ctx, llvm::map_to_vector(fnTy.getInputs(), getYulType), + llvm::map_to_vector(fnTy.getResults(), getYulType)); + }) + .Case([&](sol::ArrayType arrTy) -> Type { + switch (arrTy.getDataLocation()) { + case sol::DataLocation::Stack: + return LLVM::LLVMArrayType::get(getYulType(arrTy.getEltType()), + arrTy.getSize().getZExtValue()); + case sol::DataLocation::CallData: + return arrTy.isDynSized() ? pairTy : i256Ty; + case sol::DataLocation::Memory: + case sol::DataLocation::Storage: + case sol::DataLocation::Transient: + return i256Ty; + default: + llvm_unreachable("unexpected array data location"); + } + }) + .Case([&](sol::StringType strTy) -> Type { + switch (strTy.getDataLocation()) { + case sol::DataLocation::CallData: + return pairTy; + case sol::DataLocation::Memory: + case sol::DataLocation::Storage: + case sol::DataLocation::Transient: + return i256Ty; + default: + llvm_unreachable("unexpected string data location"); + } + }) + .Case([&](sol::StructType structTy) -> Type { + switch (structTy.getDataLocation()) { + case sol::DataLocation::CallData: + case sol::DataLocation::Memory: + case sol::DataLocation::Storage: + case sol::DataLocation::Transient: + return i256Ty; + default: + llvm_unreachable("unexpected struct data location"); + } + }) + .Case([&](sol::PointerType ptrTy) -> Type { + switch (ptrTy.getDataLocation()) { + case sol::DataLocation::Stack: + return LLVM::LLVMPointerType::get(ctx); + case sol::DataLocation::CallData: + case sol::DataLocation::Memory: + case sol::DataLocation::Immutable: + return i256Ty; + case sol::DataLocation::Storage: + case sol::DataLocation::Transient: + return sol::canBePacked(ptrTy.getPointeeType()) ? pairTy : i256Ty; + } + llvm_unreachable("unexpected pointer data location"); + }) + .Default([](Type ty) { return ty; }); +} + unsigned evm::getAlignment(Value ptr) { auto ty = cast(ptr.getType()); return getAlignment(static_cast(ty.getAddressSpace())); @@ -1204,12 +1280,7 @@ Value evm::Builder::genReturnDataBytes(std::optional locArg) { return ifOp.getResult(0); } -bool evm::isConstantValue(Value v) { - if (auto cast = v.getDefiningOp(); - cast && cast->getNumOperands() == 1) - v = cast->getOperand(0); - return matchPattern(v, m_Constant()); -} +bool evm::isConstantValue(Value v) { return matchPattern(v, m_Constant()); } Value evm::Builder::genMemAllocArray(sol::ArrayType arrTy, bool zeroInit, Value sizeVar, int64_t recDepth, @@ -1626,8 +1697,8 @@ Value evm::Builder::genCalldataAccessRef(Type ty, Value baseAddr, Value ptr, Value refAddr = b.create(loc, baseAddr, relOffset); auto genLengthAndStrideGuards = [&](Value dataAddr, Value length, Value stride) { - // Dynamic calldata payloads are materialized as {dataPtr, length}, so both - // the decoded length and the implied payload range must stay in bounds. + // A dynamic calldata payload is a {dataPtr, length} pair, so both the + // decoded length and the implied payload range must stay in bounds. Value invalidLength = bExt.genCmp(yul::CmpPredicate::ugt, length, bExt.genI256Const(APInt::getLowBitsSet(256, 64))); @@ -2091,8 +2162,7 @@ void evm::Builder::genCopyStringToStorage(Value src, Type ty, Value dstAddr, Location loc = locArg ? *locArg : defLoc; sol::DataLocation srcDataLoc = sol::getDataLocation(ty); - // The source is the legalized string reference: a calldata fat pointer - // stays one struct argument. + // A calldata string reference, a struct, is one argument. genHelperCall( helpersym::copyStringToStorage(srcDataLoc), {src, dstAddr}, [&](ValueRange args, Location bodyLoc) { @@ -2317,7 +2387,7 @@ void evm::Builder::genClearStorageValue(Type ty, Value slot, Location loc) { } /// Returns the helper function named \p symbol in the module's one contract, -/// creating it on first request: a `sol.func` with signature +/// creating it on first request: a private `yul.func` with signature /// `argTys -> resTys` whose body is emitted by \p genBody (which receives the /// entry-block arguments and the location to build at, line 0 since every /// caller shares the body, and must emit the body's terminator). The @@ -2326,7 +2396,7 @@ void evm::Builder::genClearStorageValue(Type ty, Value slot, Location loc) { /// reused. The symbol is registered before \p genBody runs, so a /// self-reference reached while building the body resolves to a recursive /// call. -static sol::FuncOp +static yul::FuncOp getOrCreateHelperFn(ModuleOp mod, OpBuilder &b, StringRef symbol, TypeRange argTys, TypeRange resTys, llvm::function_ref genBody) { @@ -2335,7 +2405,7 @@ getOrCreateHelperFn(ModuleOp mod, OpBuilder &b, StringRef symbol, Block *hostBlock = &contract.getBodyRegion().front(); auto fnTy = FunctionType::get(b.getContext(), argTys, resTys); - if (auto fn = dyn_cast_or_null( + if (auto fn = dyn_cast_or_null( SymbolTable::lookupSymbolIn(host, symbol))) { assert(fn.getFunctionType() == fnTy && "helper signature mismatch"); return fn; @@ -2349,8 +2419,8 @@ getOrCreateHelperFn(ModuleOp mod, OpBuilder &b, StringRef symbol, Location hostLoc = host->getLoc(); Location bodyLoc = evm::artificialLoc(hostLoc); - auto fn = b.create( - evm::fuseArtificialSubprogram(mod, symbol, hostLoc), symbol, fnTy); + auto fn = evm::createPrivateFunc( + b, evm::fuseArtificialSubprogram(mod, symbol, hostLoc), symbol, fnTy); Block *entry = &fn.getBody().emplaceBlock(); SmallVector argLocs(argTys.size(), bodyLoc); entry->addArguments(argTys, argLocs); @@ -2360,17 +2430,25 @@ getOrCreateHelperFn(ModuleOp mod, OpBuilder &b, StringRef symbol, return fn; } +yul::FuncOp evm::createPrivateFunc(OpBuilder &b, Location loc, StringRef name, + FunctionType ty) { + auto fn = b.create(loc, name, ty); + fn->setAttr("llvm.linkage", + LLVM::LinkageAttr::get(b.getContext(), LLVM::Linkage::Private)); + return fn; +} + void evm::Builder::genHelperCall( StringRef symbol, ValueRange args, llvm::function_ref genBody, std::optional locArg) { Location loc = locArg ? *locArg : defLoc; - sol::FuncOp fn = getOrCreateHelperFn(mod, b, symbol, args.getTypes(), {}, + yul::FuncOp fn = getOrCreateHelperFn(mod, b, symbol, args.getTypes(), {}, [&](ValueRange a, Location bodyLoc) { genBody(a, bodyLoc); - b.create(bodyLoc); + b.create(bodyLoc); }); - b.create(loc, fn, args); + b.create(loc, fn, args); } void evm::Builder::genHaltingHelperCall( @@ -2378,9 +2456,9 @@ void evm::Builder::genHaltingHelperCall( llvm::function_ref genBody, std::optional locArg) { Location loc = locArg ? *locArg : defLoc; - sol::FuncOp fn = + yul::FuncOp fn = getOrCreateHelperFn(mod, b, symbol, args.getTypes(), {}, genBody); - b.create(loc, fn, args); + b.create(loc, fn, args); b.create(loc); } @@ -2389,12 +2467,12 @@ Value evm::Builder::genHelperCall( llvm::function_ref genBody, std::optional locArg) { Location loc = locArg ? *locArg : defLoc; - sol::FuncOp fn = getOrCreateHelperFn( + yul::FuncOp fn = getOrCreateHelperFn( mod, b, symbol, args.getTypes(), {resTy}, [&](ValueRange a, Location bodyLoc) { - b.create(bodyLoc, ValueRange{genBody(a, bodyLoc)}); + b.create(bodyLoc, ValueRange{genBody(a, bodyLoc)}); }); - return b.create(loc, fn, args).getResult(0); + return b.create(loc, fn, args).getResult(0); } SmallVector evm::Builder::genHelperCall( @@ -2402,12 +2480,13 @@ SmallVector evm::Builder::genHelperCall( llvm::function_ref(ValueRange, Location)> genBody, std::optional locArg) { Location loc = locArg ? *locArg : defLoc; - sol::FuncOp fn = getOrCreateHelperFn(mod, b, symbol, args.getTypes(), resTys, + yul::FuncOp fn = getOrCreateHelperFn(mod, b, symbol, args.getTypes(), resTys, [&](ValueRange a, Location bodyLoc) { - b.create( + b.create( bodyLoc, genBody(a, bodyLoc)); }); - return SmallVector(b.create(loc, fn, args).getResults()); + return SmallVector( + b.create(loc, fn, args).getResults()); } void evm::Builder::genClearStorageValueInline(Type ty, Value slot, @@ -3045,7 +3124,7 @@ void evm::Builder::genPopString(Value srcAddr, Value oldData, Value length, // | | store length. | // +----------------------------+----------------------------------------------+ // | StructType | non-Stg dst: llvm_unreachable; must be | -// | | lowered by DataLocCastOpLowering. | +// | | lowered by sol.data_loc_cast. | // | | any->Stg: per-member via | // | | StructEncodeMemberReader + | // | | StructMemberWriter. | @@ -3959,28 +4038,25 @@ Value evm::Builder::genABIEncodingImplInline( } /// The arguments of a helper that encodes \p vals: every value but a constant -/// string, which the helper body re-creates. +/// string, whose bytes the helper symbol carries. static SmallVector getHelperArgs(ValueRange vals) { return llvm::filter_to_vector(vals, [](Value val) { return !isa(val.getType()); }); } -/// The values a helper body encodes for the caller's \p vals: a copy of each -/// constant string, whose op is in the caller, and every other value from -/// \p args, in order. -static SmallVector getHelperValues(OpBuilder &b, ValueRange vals, - ValueRange args, Location loc) { +/// The values a helper body encodes for the caller's \p vals: each constant +/// string as is, and every other value from \p args, in order. The body reads +/// a constant string's bytes from its op in the caller and never uses it as an +/// operand. +static SmallVector getHelperValues(ValueRange vals, ValueRange args) { SmallVector result; auto argIt = args.begin(); for (Value val : vals) { - if (!isa(val.getType())) { + if (isa(val.getType())) + result.push_back(val); + else result.push_back(*argIt++); - continue; - } - auto constant = cast(val.getDefiningOp()); - result.push_back( - b.create(loc, constant.getValueAttr())); } return result; } @@ -4021,9 +4097,8 @@ Value evm::Builder::genABIEncoding(TypeRange tys, ValueRange vals, helpersym::abiEncodeTuple(opts.padded, opts.dynamicInplace, tys, vals), args, b.getIntegerType(256), [&](ValueRange a, Location bodyLoc) { - return genABIEncodingInline( - tys, getHelperValues(b, vals, a.drop_back(), bodyLoc), a.back(), - opts, bodyLoc); + return genABIEncodingInline(tys, getHelperValues(vals, a.drop_back()), + a.back(), opts, bodyLoc); }, loc); } @@ -4361,7 +4436,6 @@ void evm::Builder::genABITupleDecoding(TypeRange tys, Value tupleStart, Value tupleEnd, std::vector &results, bool fromMem, - const TypeConverter &tyConv, std::optional locArg) { Location loc = locArg ? *locArg : defLoc; // An empty tuple is only the size check and stays inline. Every other @@ -4370,10 +4444,7 @@ void evm::Builder::genABITupleDecoding(TypeRange tys, Value tupleStart, genABITupleDecodingInline(tys, tupleStart, tupleEnd, results, fromMem, loc); return; } - // The results are the legalized element types. - SmallVector resTys; - for (Type ty : tys) - resTys.push_back(tyConv.convertType(ty)); + SmallVector resTys = llvm::map_to_vector(tys, getYulType); llvm::append_range( results, genHelperCall( @@ -4401,9 +4472,6 @@ void evm::Builder::genABITupleDecodingInline(TypeRange tys, Value tupleStart, genABITupleSizeAssert(tys, b.create(loc, tupleEnd, tupleStart), loc); - // Decode the args. - // The type of the decoded arg should be same as that of the legalized type - // (as per the type-converter) of the original type. Value headAddr = tupleStart; for (Type ty : tys) { // Resolve the head slot, then decode the element. A loop-carrying @@ -4498,8 +4566,7 @@ void evm::Builder::genRevert(TypeRange tys, ValueRange vals, genHaltingHelperCall( helpersym::revertError(signature, tys, vals), getHelperArgs(vals), [&](ValueRange a, Location bodyLoc) { - genRevertInline(tys, getHelperValues(b, vals, a, bodyLoc), signature, - bodyLoc); + genRevertInline(tys, getHelperValues(vals, a), signature, bodyLoc); }, loc); } diff --git a/mlir/lib/Conversion/SolToYul/InternalDispatch.cpp b/mlir/lib/Conversion/SolToYul/InternalDispatch.cpp index 61842e5ad1a1..21272e519816 100644 --- a/mlir/lib/Conversion/SolToYul/InternalDispatch.cpp +++ b/mlir/lib/Conversion/SolToYul/InternalDispatch.cpp @@ -89,29 +89,24 @@ static SmallVector regroup(ValueRange vals, TypeRange tys, Location loc, return regrouped; } -void evm::InternalDispatch::addCreationFuncRefs(sol::ContractOp contr) { - for (auto sym : contr.getCreationFuncRefs().getAsRange()) - addTarget(contr.lookupSymbol(sym)); +void evm::InternalDispatch::addTarget(yul::FuncOp fn, int64_t id) { + targets[getWordCounts(fn.getFunctionType())].insert(fn); + ids[fn] = id; } -void evm::InternalDispatch::addTarget(sol::FuncOp fn) { - auto loweredTy = cast(tyConv.convertType(fn.getFunctionType())); - targets[getWordCounts(loweredTy)].insert(fn); -} - -yul::FuncOp evm::InternalDispatch::getOrCreateDispatchFn( - sol::ICallOp icall, FunctionType dispatchFnTy, PatternRewriter &r) { +yul::FuncOp +evm::InternalDispatch::getOrCreateDispatchFn(FunctionType dispatchFnTy, + yul::FuncOp caller, OpBuilder &b) { auto [it, inserted] = requests.try_emplace(dispatchFnTy); Request &request = it->second; if (inserted) { - auto caller = icall->getParentOfType(); ModuleOp mod = getModule(caller); std::string name = evm::helpersym::internalDispatch(requests.size() - 1); // The dispatch function is shared by every call of its signature: its // subprogram is artificial at the line of the module hosting it. - OpBuilder::InsertionGuard insertGuard(r); - r.setInsertionPointAfter(caller); - auto fn = r.create( + OpBuilder::InsertionGuard insertGuard(b); + b.setInsertionPointAfter(caller); + auto fn = b.create( evm::fuseArtificialSubprogram(mod, name, mod.getLoc()), name, dispatchFnTy); request = {fn, caller}; @@ -134,11 +129,11 @@ void evm::InternalDispatch::genDispatchFns() { dispatchFnTy.getResults()); SmallVector caseIds; - SmallVector caseFns; + SmallVector caseFns; auto found = targets.find(getWordCounts(calleeTy)); if (found != targets.end()) { - for (sol::FuncOp target : found->second) { - caseIds.push_back(*target.getId()); + for (yul::FuncOp target : found->second) { + caseIds.push_back(ids.lookup(target)); caseFns.push_back(target); } } @@ -173,8 +168,8 @@ void evm::InternalDispatch::genDispatchFns() { } b.setInsertionPointToStart(&switchOp.getDefaultRegion().emplaceBlock()); - // The dispatch is built after stage 2, outside any contract, so no helper - // can serve it: the panic stays inline. + // The dispatch is built once the contract is gone, so no helper can serve + // it: the panic stays inline. evmB.genPanicInline( solgen::BuilderExt(b, loc).genI256Const(static_cast( mlir::evm::PanicCode::InvalidInternalFunction)), diff --git a/mlir/lib/Conversion/SolToYul/SolToYul.cpp b/mlir/lib/Conversion/SolToYul/SolToYul.cpp index 33c3771787d0..7f3757b97f5b 100644 --- a/mlir/lib/Conversion/SolToYul/SolToYul.cpp +++ b/mlir/lib/Conversion/SolToYul/SolToYul.cpp @@ -1,4 +1,4 @@ -//===- SolToYul.cpp - Sol to Yul dialect conversion -----------------------===// +//===- SolToYul.cpp - Sol to Yul lowering ---------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -6,7 +6,9 @@ // //===----------------------------------------------------------------------===// // -// EVM specific lowering patterns for the Sol dialect. +// Lowers the Sol contract of a module to its Yul object in one walk over its +// functions. An op is lowered after the ops defining its operands, so each Sol +// value is looked up for the Yul value it lowered to. // //===----------------------------------------------------------------------===// @@ -21,4688 +23,3652 @@ #include "mlir/Dialect/Yul/Yul.h" #include "mlir/IR/Builders.h" #include "mlir/IR/BuiltinAttributes.h" +#include "mlir/IR/IRMapping.h" #include "mlir/IR/Matchers.h" -#include "mlir/IR/PatternMatch.h" #include "mlir/IR/SymbolTable.h" -#include "mlir/IR/Types.h" -#include "mlir/Interfaces/FunctionInterfaces.h" -#include "mlir/Transforms/DialectConversion.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/Twine.h" +#include "llvm/ADT/TypeSwitch.h" #include "llvm/Support/ErrorHandling.h" #include -#include + +namespace mlir { +#define GEN_PASS_DEF_CONVERTSOLTOYULPASS +#include "mlir/Conversion/Passes.h.inc" +} // namespace mlir using namespace mlir; using solgen::getModule; -namespace { - -// Compute the types to hand to the generic ABI helpers. -// -// For normal ABI calls we keep the original source types, because the encoder -// still needs to know where values come from (for example storage or calldata -// when copying into memory ABI form). -// -// Library ABI is special only when the boundary type itself is a storage ref, -// in which case we need to use the uint256 type. -SmallVector getABITypes(OpBuilder &b, TypeRange sourceTys, - TypeRange boundaryTys, bool useLibraryABI) { - assert(sourceTys.size() == boundaryTys.size() && - "expected source and boundary types to align"); - - if (!useLibraryABI) - return SmallVector(sourceTys); - - SmallVector mappedTys; - mappedTys.reserve(boundaryTys.size()); - for (auto [sourceTy, boundaryTy] : llvm::zip(sourceTys, boundaryTys)) { - if (sol::getDataLocation(boundaryTy) == sol::DataLocation::Storage) - mappedTys.push_back(b.getIntegerType(256)); - else - mappedTys.push_back(sourceTy); - } - return mappedTys; +LLVM::GlobalOp evm::StringLiteralPool::getOrCreate(StringRef literal, + sol::ContractOp contr, + OpBuilder &b, Location loc) { + auto [it, inserted] = globals.try_emplace(literal); + if (!inserted) + return it->second; + std::string name = ("__data_in_code." + Twine(globals.size() - 1)).str(); + OpBuilder::InsertionGuard guard(b); + b.setInsertionPointToStart(contr.getBody()); + it->second = b.create( + loc, LLVM::LLVMArrayType::get(b.getIntegerType(8), literal.size()), + /*isConstant=*/true, LLVM::Linkage::Private, name, + b.getStringAttr(literal), /*alignment=*/0, /*addrSpace=*/4); + it->second.setUnnamedAddr(LLVM::UnnamedAddr::Global); + return it->second; } -template -struct CleanedOperandsLowering : public OpConversionPattern { - using Base = OpConversionPattern; - using OpAdaptor = typename Base::OpAdaptor; - using Base::Base; +namespace { + +/// Lowers a module's one contract to its Yul object. +class Lowering { +public: + explicit Lowering(sol::ContractOp contr) + : contr(contr), mod(getModule(contr)), b(contr.getContext()) {} - LogicalResult matchAndRewrite(OpT op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - evm::Builder evmB(getModule(op), r, loc); + void run(); - SmallVector cleaned; - cleaned.reserve(adaptor.getOperands().size()); - for (auto [original, converted] : - llvm::zip(op->getOperands(), adaptor.getOperands())) - cleaned.push_back(evmB.genCleanup(original.getType(), converted, loc)); +private: + sol::ContractOp contr; + ModuleOp mod; + OpBuilder b; + /// The Yul value of each lowered Sol value. + IRMapping valMap; + /// The yul.func a function of the contract lowers to, and its pointer value. + struct Func { + yul::FuncOp fn; + std::optional id; + }; + /// The contract's functions by name. A yul.func shares its name with its + /// sol.func until the contract is lowered, so it is reached through here, + /// never through the symbol table. + DenseMap funcs; + /// The function whose body is being lowered. + yul::FuncOp curFn; + evm::InternalDispatch dispatch; + evm::StringLiteralPool literalPool; - return dispatch(op, r, cleaned); + /// Returns the Yul value of `v`. + Value lowered(Value v) const { + Value yulVal = valMap.lookupOrNull(v); + assert(yulVal && "operand used before its definition is lowered"); + return yulVal; } -private: - template - LogicalResult dispatch(OpT op, ConversionPatternRewriter &r, - ArrayRef cleaned) const { - using FnT = decltype(&D::rewriteCleaned); - if constexpr (std::is_invocable_r_v>) { - return static_cast(this)->rewriteCleaned(op, r, cleaned); - } else { - using Traits = llvm::function_traits; + /// Returns the Yul values of `vs`. + SmallVector lowered(ValueRange vs) const { + return llvm::map_to_vector(vs, [&](Value v) { return lowered(v); }); + } - // rewriteCleaned(op, r, cleaned...) - constexpr size_t fixedArgs = 2; - static_assert(Traits::num_args >= fixedArgs, - "rewriteCleaned is missing required prefix arguments"); - constexpr size_t numCleaned = Traits::num_args - fixedArgs; + /// Maps `solVal` to its Yul value `yulVal`. + void mapLowered(Value solVal, Value yulVal) { + assert(!valMap.contains(solVal) && "value lowered twice"); + valMap.map(solVal, yulVal); + } - assert(cleaned.size() == numCleaned && - "rewriteCleaned signature must match op operand count"); + /// Maps each of `solVals` to its Yul value in `yulVals`. + void mapLowered(ValueRange solVals, ValueRange yulVals) { + for (auto [solVal, yulVal] : llvm::zip_equal(solVals, yulVals)) + mapLowered(solVal, yulVal); + } - return dispatchImpl(op, r, cleaned, - std::make_index_sequence{}); - } + /// Returns the declared function `name`. + Func getFunc(StringAttr name) const { + auto it = funcs.find(name); + assert(it != funcs.end() && "function used before it is declared"); + return it->second; } - template - LogicalResult dispatchImpl(OpT op, ConversionPatternRewriter &r, - ArrayRef cleaned, - std::index_sequence) const { - return static_cast(this)->rewriteCleaned(op, r, cleaned[I]...); + /// Returns an EVM builder at `loc`. + evm::Builder evmBuilder(Location loc) { + return evm::Builder(mod, b, loc, literalPool); } -}; -struct ConstantOpLowering : public OpRewritePattern { - using OpRewritePattern::OpRewritePattern; + /// Declares the yul.func of `fn`. + void declare(sol::FuncOp fn); - LogicalResult matchAndRewrite(sol::ConstantOp op, - PatternRewriter &r) const override { - Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - APInt value = cast(op.getValue()).getValue(); - auto srcTy = cast(op.getType()); + /// Lowers the body of `fn` into its yul.func. + void lowerBody(sol::FuncOp fn); - assert(value.getBitWidth() <= 256 && "Expected constant width <= 256"); - if (value.getBitWidth() < 256) - value = srcTy.isSigned() ? value.sext(256) : value.zext(256); + /// Lowers the ops of `src` at the insertion point. + void lowerBlock(Block &src); - r.replaceOp(op, bExt.genI256Const(value)); - return success(); - } -}; + /// Lowers each region of `src` into a block of the region of `dst` at the + /// same position. + void lowerRegions(Operation *src, Operation *dst); -struct FuncConstantOpLowering : public OpRewritePattern { - FuncConstantOpLowering(MLIRContext *ctx, evm::InternalDispatch &dispatch) - : OpRewritePattern(ctx), dispatch(dispatch) {} - - LogicalResult matchAndRewrite(sol::FuncConstantOp op, - PatternRewriter &r) const override { - mlir::solgen::BuilderExt bExt(r, op.getLoc()); - auto fn = - SymbolTable::lookupNearestSymbolFrom(op, op.getSymAttr()); - std::optional id = fn.getId(); - assert(id); - dispatch.addTarget(fn); - r.replaceOp(op, bExt.genI256Const(*id)); - return success(); - } + /// Lowers `op`. + void lowerOp(Operation &op); - evm::InternalDispatch &dispatch; -}; + /// Lowers `op` to what its folder computes, if it folds. A commutative op + /// may fold in place first, moving a constant operand to the right. + bool lowerFolded(Operation &op); -struct DefaultFuncConstantOpLowering - : public OpRewritePattern { - using OpRewritePattern::OpRewritePattern; + /// Returns the Yul values of the operands of `op`, cleaned for their Sol + /// types. + SmallVector getCleaned(Operation *op); - LogicalResult matchAndRewrite(sol::DefaultFuncConstantOp op, - PatternRewriter &r) const override { - mlir::solgen::BuilderExt bExt(r, op.getLoc()); - r.replaceOp(op, bExt.genI256Const(0)); - return success(); + /// Generates field `field` of the {word, word} pair `pair`. + Value genPairField(Value pair, int64_t field, Location loc) { + return b.create(loc, b.getIntegerType(256), pair, + b.getDenseI64ArrayAttr({field})); } -}; -struct ExtFuncConstantOpLowering - : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::ExtFuncConstantOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - auto i256Ty = r.getIntegerType(256); - auto structTy = - LLVM::LLVMStructType::getLiteral(r.getContext(), {i256Ty, i256Ty}); - Value undef = r.create(loc, structTy); - Value selector = bExt.genI256Const(op.getSelector()); - Value withAddr = r.create( - loc, undef, adaptor.getAddr(), r.getDenseI64ArrayAttr({0})); - Value full = r.create(loc, withAddr, selector, - r.getDenseI64ArrayAttr({1})); - r.replaceOp(op, full); - return success(); + // Arithmetic + + Value lower(sol::ConstantOp op); + /// Lowers the binary op `op` to a `YulOpT` of its operands. + template + Value lowerBinOp(OpT op) { + return b.create(op.getLoc(), lowered(op.getLhs()), + lowered(op.getRhs())); } -}; + // old codegen begin + // (Via-IR cleans output of these ops). + Value lower(sol::AddOp op) { return lowerBinOp(op); } + Value lower(sol::SubOp op) { return lowerBinOp(op); } + Value lower(sol::MulOp op) { return lowerBinOp(op); } + Value lower(sol::AndOp op) { return lowerBinOp(op); } + Value lower(sol::OrOp op) { return lowerBinOp(op); } + Value lower(sol::XorOp op) { return lowerBinOp(op); } + // old codegen end + Value lower(sol::NotOp op); + /// Lowers the division or modulo `op` to a `SignedOpT` or `UnsignedOpT` of + /// its cleaned operands, after the division-by-zero panic. + template + Value lowerDivOrMod(OpT op); + // Signed div/mod use the EVM sdiv/smod ops (defined wrapping on INT_MIN/-1) + // rather than the arith family, whose LLVM sdiv/srem are UB on that + // overflow. Unsigned has no such overflow, so the arith ops are fine there. + Value lower(sol::DivOp op) { + return lowerDivOrMod(op); + } + Value lower(sol::ModOp op) { + return lowerDivOrMod(op); + } + Value lower(sol::ExpOp op); + Value lower(sol::ShlOp op); + Value lower(sol::ShrOp op); + Value lower(sol::CmpOp op); + /// Lowers the modular `op` to a `YulOpT` of its cleaned operands, after the + /// division-by-zero panic. + template + Value lowerModArith(OpT op); + Value lower(sol::AddModOp op) { return lowerModArith(op); } + Value lower(sol::MulModOp op) { return lowerModArith(op); } + Value lower(sol::CAddOp op); + Value lower(sol::CSubOp op); + Value lower(sol::CMulOp op); + Value lower(sol::CDivOp op); + /// Generates the loop of the checked exponentiation: it multiplies `initPow` + /// by `initBase` raised to `initExp`, panicking on a power above `maxPow`, + /// and returns the power and the base it reached. + std::pair genExpHelper(Location loc, Value initPow, + Value initBase, Value initExp, + Value maxPow); + /// Generates the checked `base ** exp` of the unsigned type of `op`. + Value genExpUnsigned(sol::CExpOp op, Value base, Value exp); + /// Generates the checked `base ** exp` of the signed type of `op`. + Value genExpSigned(sol::CExpOp op, Value base, Value exp); + Value lower(sol::CExpOp op); + + // Casts + + Value lower(sol::CastOp op); + Value lower(sol::AddressCastOp op); + Value lower(sol::ContractCastOp op); + Value lower(sol::EnumCastOp op); + Value lower(sol::BytesCastOp op); + Value lower(sol::DynBytesToFixedBytesOp op); + Value lower(sol::FixedBytesIndexOp op); + Value lower(sol::ExtFuncConstantOp op); + Value lower(sol::ExtFuncAddrOp op); + Value lower(sol::ExtFuncSelectorOp op); + + // Crypto + + Value lower(sol::Keccak256Op op); + Value lower(sol::Erc7201Op op); + /// Lowers the hash `op` to a static call of the precompile at + /// `precompileAddr`. + template + Value lowerHashPrecompile(OpT op, uint32_t precompileAddr); + Value lower(sol::Sha256Op op) { return lowerHashPrecompile(op, 2); } + Value lower(sol::Ripemd160Op op) { return lowerHashPrecompile(op, 3); } + Value lower(sol::EcrecoverOp op); + + // Environment + + Value lower(sol::ThisOp op) { return b.create(op.getLoc()); } + Value lower(sol::CallerOp op) { return b.create(op.getLoc()); } + Value lower(sol::OriginOp op) { return b.create(op.getLoc()); } + Value lower(sol::GasPriceOp op) { + return b.create(op.getLoc()); + } + Value lower(sol::CallValueOp op) { + return b.create(op.getLoc()); + } + Value lower(sol::BaseFeeOp op) { + return b.create(op.getLoc()); + } + Value lower(sol::BlobBaseFeeOp op) { + return b.create(op.getLoc()); + } + Value lower(sol::ChainIdOp op) { + return b.create(op.getLoc()); + } + Value lower(sol::CoinbaseOp op) { + return b.create(op.getLoc()); + } + Value lower(sol::DifficultyOp op) { + return b.create(op.getLoc()); + } + Value lower(sol::GasLimitOp op) { + return b.create(op.getLoc()); + } + Value lower(sol::BlockNumberOp op) { + return b.create(op.getLoc()); + } + Value lower(sol::PrevRandaoOp op) { + return b.create(op.getLoc()); + } + Value lower(sol::TimestampOp op) { + return b.create(op.getLoc()); + } + Value lower(sol::GasLeftOp op) { return b.create(op.getLoc()); } + Value lower(sol::SigOp op); + Value lower(sol::BlockHashOp op); + Value lower(sol::BlobHashOp op); + void lower(sol::SelfdestructOp op); + Value lower(sol::LibAddrOp op); + Value lower(sol::CodeHashOp op); + Value lower(sol::BalanceOp op); + + void lower(sol::AssertOp op); + void lower(sol::RequireOp op); + /// A constant string stays the Sol value its consumers read the bytes of. + Value lower(sol::ConstantStringOp op) { return op; } + + // Control flow + + void lower(sol::IfOp op); + void lower(sol::SwitchOp op); + void lower(sol::WhileOp op); + void lower(sol::DoWhileOp op); + void lower(sol::ForOp op); + void lower(sol::ScopeOp op); + void lower(sol::LeaveOp op); + void lower(sol::YieldOp op); + void lower(sol::ConditionOp op); + void lower(sol::BreakOp op); + void lower(sol::ContinueOp op); + + // Functions + + SmallVector lower(sol::CallOp op); + void lower(sol::ReturnOp op); + + // Memory and storage + + Value lower(sol::AllocaOp op); + Value lower(sol::MallocOp op); + Value lower(sol::ArrayLitOp op); + Value lower(sol::StringLitOp op); + Value lower(sol::ConcatOp op); + Value lower(sol::GetCallDataOp op); + Value lower(sol::DefaultCallDataOp op); + Value lower(sol::DefaultStorageOp op); + Value lower(sol::PushOp op); + void lower(sol::PushStringOp op); + void lower(sol::PopOp op); + Value lower(sol::AddrOfOp op); + Value lower(sol::GepOp op); + Value lower(sol::MapOp op); + Value lower(sol::LoadOp op); + Value lower(sol::LoadImmutableOp op); + void lower(sol::StoreOp op); + Value lower(sol::DataLocCastOp op); + Value lower(sol::LengthOp op); + Value lower(sol::SliceOp op); + void lower(sol::CopyOp op); + void lower(sol::DeleteOp op); + + // ABI + + Value lower(sol::EncodeOp op); + SmallVector lower(sol::DecodeOp op); + void lower(sol::EmitOp op); + void lower(sol::RevertOp op); + + // External calls + + /// An external call: its Sol arguments `ins` to the callee inputs + /// `calleeInputTys`, its Sol results `resultTys` after the status, and the + /// Yul values of the rest. + struct ExtCall { + ValueRange ins; + TypeRange calleeInputTys; + TypeRange resultTys; + Value addr; + Type addrTy; + Value selector, gas, val; + bool tryCall, staticCall, delegateCall, libraryCall; + }; + /// Generates `call` and returns its status and results. + SmallVector genExtCall(const ExtCall &call, Location loc); + SmallVector lower(sol::ExtCallOp op); + SmallVector lower(sol::ExtICallOp op); + /// Lowers the bare call `op` to a `YulCallOpT` and returns its status and + /// return data. + template + SmallVector lowerBareCall(OpT op); + SmallVector lower(sol::BareCallOp op) { + return lowerBareCall(op); + } + SmallVector lower(sol::BareDelegateCallOp op) { + return lowerBareCall(op); + } + SmallVector lower(sol::BareStaticCallOp op) { + return lowerBareCall(op); + } + /// Generates the value transfer of `op` with the call stipend and returns + /// its status compared to zero by `pred`. + template + Value genValueTransferStatus(OpT op, yul::CmpPredicate pred); + Value lower(sol::SendOp op); + void lower(sol::TransferOp op); + Value lower(sol::NewOp op); + Value lower(sol::ObjectCodeOp op); + Value lower(sol::CodeOp op); + Value lower(sol::CodeSizeOp op); + + // Try + + void lower(sol::TryOp op); + /// A clause of a try: its entry block is lowered at the end of `entry`, its + /// other blocks at the end of `rest`. + struct Clause { + Region *src; + Block *entry; + Region *rest; + }; + /// Lowers `clauses` in order. + void lowerClauses(ArrayRef clauses); + + // Internal function pointers + + Value lower(sol::FuncConstantOp op); + Value lower(sol::DefaultFuncConstantOp op); + SmallVector lower(sol::ICallOp op); + + // Inline assembly + + void lower(sol::InlineAsmOp op); + /// Returns `ptr` as the !yul.ptr inline assembly takes, cast at the + /// insertion point. + Value getYulPtr(Value ptr, Location loc); + /// Lowers `op`, the address of field `field` of the {word, word} pair at its + /// source, to the !yul.ptr of the field. + template + Value lowerPairFieldPtr(OpT op, int field); + Value lower(sol::YulPtrCastOp op) { + return getYulPtr(lowered(op.getSrc()), op.getLoc()); + } + Value lower(sol::YulValCastOp op) { return lowered(op.getSrc()); } + Value lower(sol::YulStorageSlotOp op) { + return getYulPtr(lowered(op.getSrc()), op.getLoc()); + } + Value lower(sol::YulCallDataOffsetOp op) { return lowerPairFieldPtr(op, 0); } + Value lower(sol::YulCallDataLengthOp op) { return lowerPairFieldPtr(op, 1); } + Value lower(sol::YulFuncAddrOp op) { return lowerPairFieldPtr(op, 0); } + Value lower(sol::YulSelectorOp op) { return lowerPairFieldPtr(op, 1); } + Value lower(sol::YulStorageOffsetOp op); + Value lower(sol::YulStateVarSlotOp op); + Value lower(sol::YulStateVarOffsetOp op); + + // Contract -struct ExtFuncAddrOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; + /// The functions a contract's code calls: the constructor, the interface + /// functions with their selectors, and receive and fallback. + struct ContractFns { + sol::FuncOp ctor, receive, fallback; + SmallVector ifc; + SmallVector selectors; + }; - LogicalResult matchAndRewrite(sol::ExtFuncAddrOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - auto i256Ty = r.getIntegerType(256); - r.replaceOpWithNewOp(op, i256Ty, adaptor.getFunc(), - r.getDenseI64ArrayAttr({0})); - return success(); - } -}; + /// Generates a call of the contract's function `callee`. + yul::FuncCallOp genCall(StringAttr callee, ValueRange args, Location loc); -struct ExtFuncSelectorOpLowering - : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::ExtFuncSelectorOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - - // Result is bytes4, which is represented as MSB-aligned in i256 (bits - // [255:224] = selector) - auto i256Ty = r.getIntegerType(256); - Value sel = r.create(loc, i256Ty, adaptor.getFunc(), - r.getDenseI64ArrayAttr({1})); - r.replaceOpWithNewOp(op, sel, bExt.genI256Const(224)); - return success(); - } -}; + /// Generates the call value check. + void genCallValChk(Location loc); -struct CastOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; + /// Generates the free pointer initialization. A memoryguard lets the backend + /// claim the memory below it for spills, which memory-unsafe inline assembly + /// rules out. + void genFreePtrInit(Location loc, size_t reservedMem = 0); - LogicalResult matchAndRewrite(sol::CastOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - evm::Builder evmB(getModule(op), r, loc); - auto inpTyWidth = cast(op.getInp().getType()).getWidth(); - auto outTyWidth = cast(op.getType()).getWidth(); + /// Generates the dispatch to the interface functions. + void genDispatch(ContractFns &fns, Location loc); - Value val = adaptor.getInp(); - // old codegen begin - // Match old codegen's plain conversion behavior: widening has to clean the - // source width first, while narrowing is just a typed view. Semantic - // consumers such as comparisons, ABI, returns, and stores perform cleanup - // at their own boundaries. - // (Via-IR cleans in both directions). - if (outTyWidth > inpTyWidth) - val = evmB.genCleanup(op.getInp().getType(), val, loc); - // old codegen end + /// Generates the creation code: it runs the constructor `ctor`, if any, then + /// returns the runtime object's code with the immutables set. + void genCreationCode(sol::FuncOp ctor, StringRef runtimeObjName, + Location loc); - r.replaceOp(op, val); - return success(); - } + /// Generates the runtime code: the dispatch to the interface functions, then + /// receive and fallback. + void genRuntimeCode(ContractFns &fns, Location loc); + + /// Replaces the contract with its object. + void lowerContract(); }; -struct AddressCastOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::AddressCastOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - evm::Builder evmB(getModule(op), r, loc); - - Type inpTy = op.getInp().getType(); - Type outTy = op.getType(); - - if (isa(inpTy) || isa(outTy)) { - // bytes20 -> address - if (auto inpBytesTy = dyn_cast(inpTy)) { - assert(inpBytesTy.getSize() == 20 && - "AddressCastOp only supports bytes20"); - assert(isa(outTy)); - r.replaceOpWithNewOp(op, adaptor.getInp(), - bExt.genI256Const(96)); - return success(); - } +} // namespace - // address -> bytes20 - assert(cast(outTy).getSize() == 20 && - "AddressCastOp only supports bytes20"); - assert(isa(inpTy)); - r.replaceOpWithNewOp(op, adaptor.getInp(), - bExt.genI256Const(96)); - return success(); - } +//===----------------------------------------------------------------------===// +// Walk +//===----------------------------------------------------------------------===// - // old codegen begin - // (Via-IR adds cleanup helpers around address casts), - if (isa(inpTy) || isa(outTy)) { - // uint160 -> address - if (isa(inpTy)) { - [[maybe_unused]] auto inpIntTy = cast(inpTy); - assert(inpIntTy.getWidth() == 160 && !inpIntTy.isSigned() && - "AddressCastOp only supports uint160 -> address"); - assert(isa(outTy)); - } else { - // address -> uint160 - [[maybe_unused]] auto outIntTy = cast(outTy); - assert(isa(inpTy) && outIntTy.getWidth() == 160 && - !outIntTy.isSigned() && - "AddressCastOp only supports address -> uint160"); - } - r.replaceOp(op, adaptor.getInp()); - return success(); - } +void Lowering::run() { + SmallVector creationRefs; + for (auto sym : contr.getCreationFuncRefs().getAsRange()) + creationRefs.push_back(contr.lookupSymbol(sym)); + + // Every function is declared ahead of the bodies, which name them. + auto fns = llvm::to_vector(contr.getBody()->getOps()); + for (sol::FuncOp fn : fns) + declare(fn); + for (sol::FuncOp fn : creationRefs) + dispatch.addTarget(getFunc(fn.getSymNameAttr()).fn, *fn.getId()); + for (sol::FuncOp fn : fns) + lowerBody(fn); + + lowerContract(); + dispatch.genDispatchFns(); +} - if (isa(inpTy) && isa(outTy)) - // address -> address (payable <-> non-payable) - assert( - cast(inpTy).getPayable() != - cast(outTy).getPayable() && - "AddressCastOp only supports payable <-> non-payable address casts"); - else if (!(isa(inpTy) && isa(outTy)) && - !(isa(inpTy) && isa(outTy))) - // If not an address <-> contract cast, issue an error. - llvm_unreachable("Unexpected types for AddressCastOp"); - - r.replaceOp(op, adaptor.getInp()); - // old codegen end - return success(); - } -}; +void Lowering::declare(sol::FuncOp fn) { + OpBuilder::InsertionGuard guard(b); + b.setInsertionPointAfter(fn); + auto ty = cast(evm::getYulType(fn.getFunctionType())); + yul::FuncOp yulFn = evm::createPrivateFunc(b, fn.getLoc(), fn.getName(), ty); + if (Attribute linkage = fn->getAttr("llvm.linkage")) + yulFn->setAttr("llvm.linkage", linkage); + funcs[fn.getSymNameAttr()] = {yulFn, fn.getId()}; +} -struct ContractCastOpLowering - : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; +void Lowering::lowerBody(sol::FuncOp fn) { + curFn = getFunc(fn.getSymNameAttr()).fn; + lowerRegions(fn, curFn); + valMap.clear(); +} - LogicalResult matchAndRewrite(sol::ContractCastOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - // old codegen begin - // (Via-IR adds cleanup helpers around contract casts). - r.replaceOp(op, adaptor.getInp()); - // old codegen end - return success(); +void Lowering::lowerBlock(Block &src) { + for (Operation &op : src) { + lowerOp(op); + // The code after a lowering that ends the block, such as an inline + // assembly body that halts, is unreachable. + Block::iterator ip = b.getInsertionPoint(); + if (ip != b.getInsertionBlock()->begin() && + std::prev(ip)->hasTrait()) + return; } -}; +} -// When doing int -> enum cast, we need to do a cleanup on input and output -// type. For enum -> int, there is no need to cleanup on the output type, as the -// enum value is always in the valid range and thus also valid as an int value. -struct EnumCastOpLowering - : public CleanedOperandsLowering { - using Base = CleanedOperandsLowering; - using Base::Base; - - LogicalResult rewriteCleaned(sol::EnumCastOp op, ConversionPatternRewriter &r, - Value val) const { - if (isa(op.getType())) { - Location loc = op.getLoc(); - evm::Builder evmB(getModule(op), r, loc); - val = evmB.genCleanup(op.getType(), val, loc); +void Lowering::lowerRegions(Operation *src, Operation *dst) { + OpBuilder::InsertionGuard guard(b); + for (auto [srcRegion, dstRegion] : + llvm::zip_equal(src->getRegions(), dst->getRegions())) { + // Blocks lower in list order, which suits structured control flow, so we + // don't need RPO here. + for (Block &srcBlock : srcRegion) { + SmallVector argLocs = + llvm::map_to_vector(srcBlock.getArguments(), + [](BlockArgument arg) { return arg.getLoc(); }); + Block *dstBlock = b.createBlock( + &dstRegion, dstRegion.end(), + llvm::map_to_vector(srcBlock.getArgumentTypes(), evm::getYulType), + argLocs); + mapLowered(srcBlock.getArguments(), dstBlock->getArguments()); + lowerBlock(srcBlock); } - r.replaceOp(op, val); - return success(); } -}; - -struct BytesCastOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::BytesCastOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - evm::Builder evmB(getModule(op), r, loc); - Type inpTy = op.getInp().getType(); - Type outTy = op.getType(); - - const bool isByteToBytes1 = isa(inpTy) && - isa(outTy) && - sol::getNumBytes(outTy) == 1; - const bool isBytes1ToByte = isa(inpTy) && - sol::getNumBytes(inpTy) == 1 && - isa(outTy); - if (isByteToBytes1 || isBytes1ToByte) { - // byte <-> bytes1 is a no-op. - r.replaceOp(op, adaptor.getInp()); - return success(); - } +} - // Bytes to bytes - if (sol::isBytesLikeType(inpTy)) { - unsigned inpBytesSize = sol::getNumBytes(inpTy); - if (sol::isBytesLikeType(outTy)) { - // old codegen begin - // bytesN -> bytesM shortening is intentionally dirty for old-codegen - // stack-slot parity. Widening must clear bytes beyond the source width. - // (Via-IR cleans in both directions). - Value val = adaptor.getInp(); - if (inpBytesSize < sol::getNumBytes(outTy)) - val = evmB.genCleanup(inpTy, val, loc); - // old codegen end - - r.replaceOp(op, val); - return success(); - } +void Lowering::lowerOp(Operation &op) { + // A lowering may leave the insertion point in a region it built. + OpBuilder::InsertionGuard guard(b); - // Bytes to int - auto shiftAmt = bExt.genI256Const(256 - (8 * inpBytesSize)); - r.replaceOpWithNewOp(op, adaptor.getInp(), shiftAmt); - return success(); - } + // A Yul op of an inline assembly body is cloned over its lowered operands; + // its regions are lowered in turn. + if (!isa(op.getDialect())) { + assert(llvm::all_of(op.getOperands(), + [&](Value v) { return valMap.contains(v); }) && + "operand used before its definition is lowered"); + lowerRegions(&op, b.cloneWithoutRegions(op, valMap)); + return; + } + + if (lowerFolded(op)) + return; + + TypeSwitch(&op) + .Case< + // clang-format off + sol::ConstantOp, + sol::AddOp, + sol::SubOp, + sol::MulOp, + sol::AndOp, + sol::OrOp, + sol::XorOp, + sol::NotOp, + sol::DivOp, + sol::ModOp, + sol::ExpOp, + sol::ShlOp, + sol::ShrOp, + sol::CmpOp, + sol::AddModOp, + sol::MulModOp, + sol::CAddOp, + sol::CSubOp, + sol::CMulOp, + sol::CDivOp, + sol::CExpOp, + sol::CastOp, + sol::AddressCastOp, + sol::ContractCastOp, + sol::EnumCastOp, + sol::BytesCastOp, + sol::DynBytesToFixedBytesOp, + sol::FixedBytesIndexOp, + sol::ExtFuncConstantOp, + sol::ExtFuncAddrOp, + sol::ExtFuncSelectorOp, + sol::Keccak256Op, + sol::Erc7201Op, + sol::Sha256Op, + sol::Ripemd160Op, + sol::EcrecoverOp, + sol::ThisOp, + sol::CallerOp, + sol::OriginOp, + sol::GasPriceOp, + sol::CallValueOp, + sol::BaseFeeOp, + sol::BlobBaseFeeOp, + sol::ChainIdOp, + sol::CoinbaseOp, + sol::DifficultyOp, + sol::GasLimitOp, + sol::BlockNumberOp, + sol::PrevRandaoOp, + sol::TimestampOp, + sol::GasLeftOp, + sol::SigOp, + sol::BlockHashOp, + sol::BlobHashOp, + sol::SelfdestructOp, + sol::LibAddrOp, + sol::CodeHashOp, + sol::BalanceOp, + sol::AssertOp, + sol::RequireOp, + sol::ConstantStringOp, + sol::IfOp, + sol::SwitchOp, + sol::WhileOp, + sol::DoWhileOp, + sol::ForOp, + sol::ScopeOp, + sol::LeaveOp, + sol::YieldOp, + sol::ConditionOp, + sol::BreakOp, + sol::ContinueOp, + sol::CallOp, + sol::ReturnOp, + sol::AllocaOp, + sol::MallocOp, + sol::ArrayLitOp, + sol::StringLitOp, + sol::ConcatOp, + sol::GetCallDataOp, + sol::DefaultCallDataOp, + sol::DefaultStorageOp, + sol::PushOp, + sol::PushStringOp, + sol::PopOp, + sol::AddrOfOp, + sol::GepOp, + sol::MapOp, + sol::LoadOp, + sol::LoadImmutableOp, + sol::StoreOp, + sol::DataLocCastOp, + sol::LengthOp, + sol::SliceOp, + sol::CopyOp, + sol::DeleteOp, + sol::EncodeOp, + sol::DecodeOp, + sol::EmitOp, + sol::RevertOp, + sol::ExtCallOp, + sol::ExtICallOp, + sol::BareCallOp, + sol::BareDelegateCallOp, + sol::BareStaticCallOp, + sol::SendOp, + sol::TransferOp, + sol::NewOp, + sol::ObjectCodeOp, + sol::CodeOp, + sol::CodeSizeOp, + sol::TryOp, + sol::FuncConstantOp, + sol::DefaultFuncConstantOp, + sol::ICallOp, + sol::InlineAsmOp, + sol::YulPtrCastOp, + sol::YulValCastOp, + sol::YulStorageSlotOp, + sol::YulCallDataOffsetOp, + sol::YulCallDataLengthOp, + sol::YulFuncAddrOp, + sol::YulSelectorOp, + sol::YulStorageOffsetOp, + sol::YulStateVarSlotOp, + sol::YulStateVarOffsetOp + // clang-format on + >([&](auto op) { + // A lowering returns the Yul value of each result: a Value for a + // single-result op, a SmallVector for a variadic one, nothing + // for an op without results. + using ResultT = decltype(lower(op)); + if constexpr (std::is_same_v) { + assert(op->getNumResults() == 1 && "expected a single-result op"); + mapLowered(op->getResult(0), lower(op)); + } else if constexpr (std::is_same_v>) { + SmallVector results = lower(op); + assert(results.size() == op->getNumResults() && + "expected a Yul value per result"); + mapLowered(op->getResults(), results); + } else { + static_assert(std::is_void_v); + assert(op->getNumResults() == 0 && "expected an op without results"); + lower(op); + } + }) + .Default([](Operation *) { + llvm_unreachable("unexpected op in a function body"); + }); +} - // Int to bytes - assert(isa(inpTy)); - unsigned outBytesSize = sol::getNumBytes(outTy); - auto shiftAmt = bExt.genI256Const(256 - (8 * outBytesSize)); - r.replaceOpWithNewOp(op, adaptor.getInp(), shiftAmt); - return success(); - } -}; +bool Lowering::lowerFolded(Operation &op) { + if (matchPattern(&op, m_Constant())) + return false; -struct DynBytesToFixedBytesOpLowering - : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::DynBytesToFixedBytesOp op, - OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - evm::Builder evmB(getModule(op), r, op.getLoc()); - auto dstTy = cast(op.getType()); - r.replaceOp(op, evmB.genDynBytesToFixedBytes(adaptor.getInp(), - op.getInp().getType(), dstTy, - op.getLoc())); - return success(); - } -}; + SmallVector folded; + do { + if (failed(op.fold(folded))) + return false; + } while (folded.empty()); + + for (auto [res, foldRes] : llvm::zip_equal(op.getResults(), folded)) { + if (auto val = dyn_cast(foldRes)) { + mapLowered(res, lowered(val)); + continue; + } + // As in the conversion driver, a folded attribute is materialized as a Sol + // constant, which lowers like any other op. + Operation *constOp = op.getDialect()->materializeConstant( + b, cast(foldRes), res.getType(), op.getLoc()); + assert(constOp && "folded attribute the dialect cannot materialize"); + assert(constOp->getNumResults() == 1 && + "expected a single-result constant"); + lowerOp(*constOp); + mapLowered(res, lowered(constOp->getResult(0))); + constOp->erase(); + } + return true; +} -struct FixedBytesIndexOpLowering - : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::FixedBytesIndexOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - evm::Builder evmB(getModule(op), r, loc); - - unsigned numBytes = - cast(op.getValue().getType()).getSize(); - // Only the index is cleaned. The bounds check keeps `byte` within the - // leading numBytes bytes, and dirty bits only sit in the trailing ones. - Value idx = - evmB.genCleanup(op.getIndex().getType(), adaptor.getIndex(), loc); - Value val = adaptor.getValue(); - - // Bounds check, mirroring the legacy codegen: index >= numBytes panics - // with ArrayOutOfBounds. - Value oob = bExt.genCmp(yul::CmpPredicate::uge, idx, - bExt.genI256Const(numBytes, loc), loc); - evmB.genPanic(mlir::evm::PanicCode::ArrayOutOfBounds, oob, loc); - - // `byte(idx, val)` extracts the idx-th byte (from the MSB) right-aligned; - // shift it left by 248 bits to left-align it as a bytes1 value. - Value byteVal = r.create(loc, idx, val); - Value res = r.create(loc, bExt.genI256Const(248, loc), byteVal); - r.replaceOp(op, res); - return success(); - } -}; +SmallVector Lowering::getCleaned(Operation *op) { + Location loc = op->getLoc(); + evm::Builder evmB = evmBuilder(loc); + return llvm::map_to_vector(op->getOperands(), [&](Value v) { + return evmB.genCleanup(v.getType(), lowered(v), loc); + }); +} -// old codegen begin -// (Via-IR cleans output of these ops). -/// A templatized version of a conversion pattern for lowering add, sub, mul -/// and exp ops. -template -struct ArithBinOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(SrcOpT op, typename SrcOpT::Adaptor adaptor, - ConversionPatternRewriter &r) const override { - r.replaceOpWithNewOp(op, adaptor.getLhs(), adaptor.getRhs()); - return success(); - } -}; +//===----------------------------------------------------------------------===// +// Arithmetic +//===----------------------------------------------------------------------===// -struct NotOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; +Value Lowering::lower(sol::ConstantOp op) { + return solgen::BuilderExt(b, op.getLoc()) + .genI256Const(cast(op.getValue()).getValue(), + cast(op.getType())); +} - LogicalResult matchAndRewrite(sol::NotOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - r.replaceOpWithNewOp(op, adaptor.getValue()); - return success(); - } -}; +// old codegen begin +// (Via-IR cleans output of this op). +Value Lowering::lower(sol::NotOp op) { + return b.create(op.getLoc(), lowered(op.getValue())); +} // old codegen end -/// A templatized version of a conversion pattern for lowering div and mod ops. -template -struct DivOrModOpLowering - : public CleanedOperandsLowering< - SolOp, DivOrModOpLowering> { - using Base = CleanedOperandsLowering< - SolOp, DivOrModOpLowering>; +template +Value Lowering::lowerDivOrMod(OpT op) { + Location loc = op.getLoc(); + SmallVector cleaned = getCleaned(op); + Value lhs = cleaned[0], rhs = cleaned[1]; + evmBuilder(loc).genDivisionByZeroPanic(rhs, loc); + if (cast(op.getType()).isSigned()) + return b.create(loc, lhs, rhs); + return b.create(loc, lhs, rhs); +} - using Base::Base; +Value Lowering::lower(sol::ExpOp op) { + SmallVector cleaned = getCleaned(op); + return b.create(op.getLoc(), cleaned[0], cleaned[1]); +} - LogicalResult rewriteCleaned(SolOp op, ConversionPatternRewriter &r, - Value lhs, Value rhs) const { - Location loc = op.getLoc(); - evm::Builder evmB(getModule(op), r, loc); +Value Lowering::lower(sol::ShlOp op) { + SmallVector cleaned = getCleaned(op); + Value val = cleaned[0], shift = cleaned[1]; + return b.create(op.getLoc(), shift, val); +} - auto ty = cast(op.getType()); +Value Lowering::lower(sol::ShrOp op) { + SmallVector cleaned = getCleaned(op); + Value val = cleaned[0], shift = cleaned[1]; + if (auto ty = dyn_cast(op.getType())) + if (ty.isSigned()) + return b.create(op.getLoc(), shift, val); + return b.create(op.getLoc(), shift, val); +} - evmB.genDivisionByZeroPanic(rhs, loc); +/// Returns the Yul predicate of `pred` comparing integers of signedness +/// `isSigned`. +static yul::CmpPredicate getYulPred(sol::CmpPredicate pred, bool isSigned) { + switch (pred) { + case sol::CmpPredicate::eq: + return yul::CmpPredicate::eq; + case sol::CmpPredicate::ne: + return yul::CmpPredicate::ne; + case sol::CmpPredicate::lt: + return isSigned ? yul::CmpPredicate::slt : yul::CmpPredicate::ult; + case sol::CmpPredicate::le: + return isSigned ? yul::CmpPredicate::sle : yul::CmpPredicate::ule; + case sol::CmpPredicate::gt: + return isSigned ? yul::CmpPredicate::sgt : yul::CmpPredicate::ugt; + case sol::CmpPredicate::ge: + return isSigned ? yul::CmpPredicate::sge : yul::CmpPredicate::uge; + } + llvm_unreachable("Invalid predicate"); +} - if (ty.isSigned()) - r.replaceOpWithNewOp(op, lhs, rhs); - else - r.replaceOpWithNewOp(op, lhs, rhs); - return success(); +Value Lowering::lower(sol::CmpOp op) { + Location loc = op.getLoc(); + solgen::BuilderExt bExt(b, loc); + evm::Builder evmB = evmBuilder(loc); + bool isSigned = false; + if (auto intTy = dyn_cast(op.getLhs().getType())) + isSigned = intTy.isSigned(); + yul::CmpPredicate pred = getYulPred(op.getPredicate(), isSigned); + + // An external function compares by its address and selector lanes. + if (isa(op.getLhs().getType())) { + assert((pred == yul::CmpPredicate::eq || pred == yul::CmpPredicate::ne) && + "unexpected predicate on external functions"); + auto genLaneCmp = [&](int64_t idx, unsigned numBits) { + Value mask = bExt.genI256Const(APInt::getLowBitsSet(256, numBits)); + Value lhsLane = genPairField(lowered(op.getLhs()), idx, loc); + Value rhsLane = genPairField(lowered(op.getRhs()), idx, loc); + return bExt.genCmp(pred, b.create(loc, lhsLane, mask), + b.create(loc, rhsLane, mask)); + }; + Value addrCmp = genLaneCmp(0, 160); + Value selCmp = genLaneCmp(1, 32); + if (pred == yul::CmpPredicate::eq) + return b.create(loc, addrCmp, selCmp); + return b.create(loc, addrCmp, selCmp); } -}; -struct ExpOpLowering - : public CleanedOperandsLowering { - using Base = CleanedOperandsLowering; - using Base::Base; + Value lhs = evmB.genCleanup(op.getLhs().getType(), lowered(op.getLhs()), loc); + Value rhs = evmB.genCleanup(op.getRhs().getType(), lowered(op.getRhs()), loc); + return bExt.genCmp(pred, lhs, rhs); +} - LogicalResult rewriteCleaned(sol::ExpOp op, ConversionPatternRewriter &r, - Value lhs, Value rhs) const { - r.replaceOpWithNewOp(op, lhs, rhs); - return success(); - } -}; +template +Value Lowering::lowerModArith(OpT op) { + Location loc = op.getLoc(); + SmallVector cleaned = getCleaned(op); + evmBuilder(loc).genDivisionByZeroPanic(cleaned[2], loc); + return b.create(loc, cleaned[0], cleaned[1], cleaned[2]); +} -struct CExpOpLowering - : public CleanedOperandsLowering { - using Base = CleanedOperandsLowering; - using Base::Base; - - // The genExpHelper function implements the following YUL helper function. - // - // function checked_exp_helper(power, base, exponent, max) -> power, base { - // for { } gt(exponent, 1) {} - // { - // if gt(base, div(max, base)) { panic_error_0x11() } - // if and(exponent, 1) - // { - // power := mul(power, base) - // } - // base := mul(base, base) - // exponent := shift_right_1_unsigned(exponent) - // } - // } - - std::pair genExpHelper(ModuleOp mod, - ConversionPatternRewriter &r, - Location loc, Value initPow, - Value initBase, Value initExp, - Value maxPow) const { - mlir::solgen::BuilderExt bExt(r, loc); - evm::Builder evmB(mod, r, loc); - - Value one = bExt.genI256Const(1, loc); - SmallVector whileInitVals{initPow, initBase, initExp}; - TypeRange whileArgTypes(whileInitVals); - SmallVector whileBbArgLocs(whileInitVals.size(), loc); - - // Produces power, base - auto whileOp = r.create(loc, whileArgTypes, whileInitVals); - // Condition region - { - Block *condBlock = &whileOp.getCond().emplaceBlock(); - whileOp.getCond().addArguments(whileArgTypes, whileBbArgLocs); - r.setInsertionPointToStart(condBlock); - Value curPow = condBlock->getArgument(0); - Value curBase = condBlock->getArgument(1); - Value curExp = condBlock->getArgument(2); - Value cmp = bExt.genCmp(yul::CmpPredicate::ugt, curExp, one); - r.create(loc, cmp, ValueRange{curPow, curBase, curExp}); - } +Value Lowering::lower(sol::CAddOp op) { + Location loc = op.getLoc(); + solgen::BuilderExt bExt(b, loc); + evm::Builder evmB = evmBuilder(loc); + SmallVector cleaned = getCleaned(op); + Value lhs = cleaned[0], rhs = cleaned[1]; - // Body region - { - Block *bodyBlock = &whileOp.getBody().emplaceBlock(); - whileOp.getBody().addArguments(whileArgTypes, whileBbArgLocs); - r.setInsertionPointToStart(bodyBlock); - Value curPow = bodyBlock->getArgument(0); - Value curBase = bodyBlock->getArgument(1); - Value curExp = bodyBlock->getArgument(2); - { - Value tmpDiv = r.create(loc, maxPow, curBase); - Value panicCond = bExt.genCmp(yul::CmpPredicate::ugt, curBase, tmpDiv); - evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, panicCond); - } - Value lsb = r.create(loc, curExp, one); - Value zero = bExt.genI256Const(0, loc); - Value expIsOdd = bExt.genCmp(yul::CmpPredicate::ne, lsb, zero); - Value newPow = r.create( - loc, expIsOdd, - r.create(loc, curPow, curBase), curPow) - .getResult(); - Value newBase = r.create(loc, curBase, curBase); - Value newExp = r.create(loc, curExp, one); - r.create(loc, ValueRange{newPow, newBase, newExp}); - } + auto ty = cast(op.getType()); + Value sum = b.create(loc, lhs, rhs); - return {whileOp.getResult(0), whileOp.getResult(1)}; + if (ty.getWidth() < 256) { + if (ty.isSigned()) { + // overflow, if sum > max + // underflow, if sum < min + Value max = + bExt.genI256Const(APInt::getSignedMaxValue(ty.getWidth()), ty, loc); + Value overMax = bExt.genCmp(yul::CmpPredicate::sgt, sum, max); + Value min = + bExt.genI256Const(APInt::getSignedMinValue(ty.getWidth()), ty, loc); + Value underMin = bExt.genCmp(yul::CmpPredicate::slt, sum, min); + evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, + b.create(loc, overMax, underMin)); + } else { + // overflow, if sum > max + Value max = bExt.genI256Const(APInt::getMaxValue(ty.getWidth()), ty, loc); + evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, + bExt.genCmp(yul::CmpPredicate::ugt, sum, max)); + } + return sum; } - // Below is the implementation of unsigned exponentiation based on the - // following YUL algorithm. - // - // function checked_exp_unsigned(base, exponent, max) -> power { - // if iszero(exponent) { power := 1 leave } - // if iszero(base) { power := 0 leave } - // switch base - // case 1 { power := 1 leave } - // case 2 - // { - // if gt(exponent, 255) { panic_error_0x11() } - // power := exp(2, exponent) - // if gt(power, max) { panic_error_0x11() } - // leave - // } - // if or(and(lt(base, 11), lt(exponent, 78)), - // and(lt(base, 307), lt(exponent, 32))) - // { - // power := exp(base, exponent) - // if gt(power, max) { panic_error_0x11() } - // leave - // } - // power, base := checked_exp_helper(1, base, exponent, max) - // if gt(power, div(max, base)) { panic_error_0x11() } - // power := mul(power, base) - // } - - Value expUnsigned(sol::CExpOp op, ConversionPatternRewriter &r, Value base, - Value exp) const { - Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - evm::Builder evmB(getModule(op), r, loc); - - IntegerType i256Ty = r.getIntegerType(256); - auto ty = cast(op.getType()); - Value max256 = bExt.genI256Const( - llvm::APInt::getMaxValue(ty.getWidth()).zext(256), loc); - Value zero = bExt.genI256Const(0, loc); - Value one = bExt.genI256Const(1, loc); - - auto expEqZero = bExt.genCmp(yul::CmpPredicate::eq, exp, zero); - auto baseEqOne = bExt.genCmp(yul::CmpPredicate::eq, base, one); - auto powOneCond = r.create(loc, expEqZero, baseEqOne); - - // If 1 begin - auto ifExpEqZero = bExt.createIf(i256Ty, powOneCond); - // If 1 then - r.setInsertionPointToStart(&ifExpEqZero.getThenRegion().front()); - r.create(loc, one); - - // If 1 else - r.setInsertionPointToStart(&ifExpEqZero.getElseRegion().front()); - auto baseEqZero = bExt.genCmp(yul::CmpPredicate::eq, base, zero); - - // If 2 begin - auto ifBaseEqZero = bExt.createIf(i256Ty, baseEqZero); - // If 2 then - r.setInsertionPointToStart(&ifBaseEqZero.getThenRegion().front()); - r.create(loc, zero); - - // If 2 else - r.setInsertionPointToStart(&ifBaseEqZero.getElseRegion().front()); - Value two = bExt.genI256Const(2, loc); - auto baseEqTwo = bExt.genCmp(yul::CmpPredicate::eq, base, two); - - // If 3 begin - auto ifBaseEqTwo = bExt.createIf(i256Ty, baseEqTwo); - // If 3 then - r.setInsertionPointToStart(&ifBaseEqTwo.getThenRegion().front()); - { - auto expGt255 = - bExt.genCmp(yul::CmpPredicate::ugt, exp, bExt.genI256Const(255, loc)); - evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, expGt255); + if (ty.isSigned()) { + // (Copied from the yul codegen) + // overflow, if y >= 0 and sum < x + // underflow, if y < 0 and sum >= x + // + // The rhs is compared with zero since the canonicalizer could make it a + // constant, which folds the comparison. + auto zero = bExt.genI256Const(0, loc); - auto two256 = bExt.genI256Const(2, loc); - Value tmpPow = r.create(loc, two256, exp); - auto panicCond = bExt.genCmp(yul::CmpPredicate::ugt, tmpPow, max256); - evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, panicCond); + auto rhsGtEqZero = bExt.genCmp(yul::CmpPredicate::sge, rhs, zero); + auto sumLtLhs = bExt.genCmp(yul::CmpPredicate::slt, sum, lhs); + auto overflowCond = b.create(loc, rhsGtEqZero, sumLtLhs); - r.create(loc, tmpPow); - } - // If 3 else - r.setInsertionPointToStart(&ifBaseEqTwo.getElseRegion().front()); - auto baseLT11 = - bExt.genCmp(yul::CmpPredicate::ult, base, bExt.genI256Const(11, loc)); - Value baseLT307 = (ty.getWidth() > 8) - ? bExt.genCmp(yul::CmpPredicate::ult, base, - bExt.genI256Const(307, loc)) - : bExt.genBool(true, loc); - - auto expLT78 = - bExt.genCmp(yul::CmpPredicate::ult, exp, bExt.genI256Const(78, loc)); - auto expLT32 = - bExt.genCmp(yul::CmpPredicate::ult, exp, bExt.genI256Const(32, loc)); - - auto viaExpBuitinCond = - r.create(loc, r.create(loc, baseLT11, expLT78), - r.create(loc, baseLT307, expLT32)); - // If 4 begin - auto ifViaExpBuitinCond = bExt.createIf(i256Ty, viaExpBuitinCond); - // If 4 then - r.setInsertionPointToStart(&ifViaExpBuitinCond.getThenRegion().front()); + auto rhsLtZero = bExt.genCmp(yul::CmpPredicate::slt, rhs, zero); + auto sumGtEqLhs = bExt.genCmp(yul::CmpPredicate::sge, sum, lhs); + auto underflowCond = b.create(loc, rhsLtZero, sumGtEqLhs); - { - Value tmpPow = r.create(loc, base, exp); - Value panicCond = bExt.genCmp(yul::CmpPredicate::ugt, tmpPow, max256); - evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, panicCond); + evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, + b.create(loc, overflowCond, underflowCond)); + } else { + evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, + bExt.genCmp(yul::CmpPredicate::ugt, lhs, sum)); + } + return sum; +} - r.create(loc, tmpPow); - } - // If 4 else - r.setInsertionPointToStart(&ifViaExpBuitinCond.getElseRegion().front()); - auto [pow2, base2] = - genExpHelper(getModule(op), r, loc, one, base, exp, max256); +Value Lowering::lower(sol::CSubOp op) { + Location loc = op.getLoc(); + solgen::BuilderExt bExt(b, loc); + evm::Builder evmB = evmBuilder(loc); + SmallVector cleaned = getCleaned(op); + Value lhs = cleaned[0], rhs = cleaned[1]; - { - r.setInsertionPointToEnd(&ifViaExpBuitinCond.getElseRegion().front()); - Value tmpDiv = r.create(loc, max256, base2); - Value panicCond = bExt.genCmp(yul::CmpPredicate::ugt, pow2, tmpDiv); - evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, panicCond); - Value res = r.create(loc, base2, pow2); - r.create(loc, res); + auto ty = cast(op.getType()); + Value diff = b.create(loc, lhs, rhs); + + if (ty.getWidth() < 256) { + if (ty.isSigned()) { + // overflow, if diff > max + // underflow, if diff < min + Value max = + bExt.genI256Const(APInt::getSignedMaxValue(ty.getWidth()), ty, loc); + Value overMax = bExt.genCmp(yul::CmpPredicate::sgt, diff, max); + Value min = + bExt.genI256Const(APInt::getSignedMinValue(ty.getWidth()), ty, loc); + Value underMin = bExt.genCmp(yul::CmpPredicate::slt, diff, min); + evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, + b.create(loc, overMax, underMin)); + } else { + // underflow, if diff > max + Value max = bExt.genI256Const(APInt::getMaxValue(ty.getWidth()), ty, loc); + evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, + bExt.genCmp(yul::CmpPredicate::ugt, diff, max)); } - // If 4 (ifViaExpBuitinCond) end - r.setInsertionPointToEnd(&ifBaseEqTwo.getElseRegion().front()); - r.create(loc, ifViaExpBuitinCond.getResult(0)); - // If 3 (ifBaseEqTwo) end + return diff; + } - r.setInsertionPointToEnd(&ifBaseEqZero.getElseRegion().front()); - r.create(loc, ifBaseEqTwo.getResult(0)); - // If 2 (ifBaseEqZero) end + if (ty.isSigned()) { + // (Copied from the yul codegen) + // underflow, if y >= 0 and diff > x + // overflow, if y < 0 and diff < x + auto zero = bExt.genI256Const(0, loc); - r.setInsertionPointToEnd(&ifExpEqZero.getElseRegion().front()); - r.create(loc, ifBaseEqZero.getResult(0)); - // If 1 (ifExpEqZero) end + auto rhsGtEqZero = bExt.genCmp(yul::CmpPredicate::sge, rhs, zero); + auto diffGtLhs = bExt.genCmp(yul::CmpPredicate::sgt, diff, lhs); + auto overflowCond = b.create(loc, rhsGtEqZero, diffGtLhs); - r.setInsertionPointAfter(ifExpEqZero); + auto rhsLtZero = bExt.genCmp(yul::CmpPredicate::slt, rhs, zero); + auto diffLtRhs = bExt.genCmp(yul::CmpPredicate::slt, diff, lhs); + auto underflowCond = b.create(loc, rhsLtZero, diffLtRhs); - return ifExpEqZero.getResult(0); + evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, + b.create(loc, overflowCond, underflowCond)); + } else { + evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, + bExt.genCmp(yul::CmpPredicate::ugt, diff, lhs)); } + return diff; +} - // Below is the implementation of signed exponentiation based on the - // following YUL algorithm. - // - // function checked_exp_signed(base, exponent, min, max) -> power { - // switch exponent - // case 0 { power := 1 leave } - // case 1 { power := base leave } - // if iszero(base) { power := 0 leave } - // - // power := 1 - // switch sgt(base, 0) - // case 1 { if gt(base, div(max, base)) { panic_error_0x11() } } - // case 0 { if slt(base, sdiv(max, base)) { panic_error_0x11() } } - // if and(exponent, 1) - // { - // power := base - // } - // base := mul(base, base) - // exponent := shift_right_1_unsigned(exponent) - // - // power, base := checked_exp_helper(power, base, exponent, max) - // - // if and(sgt(power, 0), gt(power, div(max, base))) { panic_error_0x11() } - // if and(slt(power, 0), slt(power, sdiv(min, base))) { panic_error_0x11() - // } power := mul(power, base) - // } - - Value expSigned(sol::CExpOp op, ConversionPatternRewriter &r, Value base, - Value exp) const { - Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - evm::Builder evmB(getModule(op), r, loc); - - IntegerType i256Ty = r.getIntegerType(256); - auto ty = cast(op.getType()); - Value zero = bExt.genI256Const(0, loc); - Value one = bExt.genI256Const(1, loc); - Value max256 = bExt.genI256Const( - llvm::APInt::getSignedMaxValue(ty.getWidth()).sext(256), loc); - Value min256 = bExt.genI256Const( - llvm::APInt::getSignedMinValue(ty.getWidth()).sext(256), loc); - - auto expEqZero = bExt.genCmp(yul::CmpPredicate::eq, exp, zero); - - // If 1 begin - auto ifExpEqZero = bExt.createIf(i256Ty, expEqZero); - // If 1 then - r.setInsertionPointToStart(&ifExpEqZero.getThenRegion().front()); - r.create(loc, one); - - // If 1 else - r.setInsertionPointToStart(&ifExpEqZero.getElseRegion().front()); - auto baseEqZero = bExt.genCmp(yul::CmpPredicate::eq, base, zero); - - // If 2 begin - auto ifBaseEqZero = bExt.createIf(i256Ty, baseEqZero); - // If 2 then - r.setInsertionPointToStart(&ifBaseEqZero.getThenRegion().front()); - r.create(loc, zero); - - // If 2 else - r.setInsertionPointToStart(&ifBaseEqZero.getElseRegion().front()); - auto expEqOne = bExt.genCmp(yul::CmpPredicate::eq, exp, one); - - // If 3 begin - auto ifExpEqOne = bExt.createIf(i256Ty, expEqOne); - // If 3 then - r.setInsertionPointToStart(&ifExpEqOne.getThenRegion().front()); - r.create(loc, base); - - // If 3 else - r.setInsertionPointToStart(&ifExpEqOne.getElseRegion().front()); - auto baseSgtZero = bExt.genCmp(yul::CmpPredicate::sgt, base, zero); - - // If 4 begin - auto ifBaseSgtZero = bExt.createIf(TypeRange{}, baseSgtZero, - /*withElseRegion=*/true); - // If 4 then - r.setInsertionPointToStart(&ifBaseSgtZero.getThenRegion().front()); - { - Value tmpDiv = r.create(loc, max256, base); - Value panicCond = bExt.genCmp(yul::CmpPredicate::ugt, base, tmpDiv); - evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, panicCond); - } - // If 4 else - r.setInsertionPointToStart(&ifBaseSgtZero.getElseRegion().front()); - { - Value tmpDiv = r.create(loc, max256, base); - Value panicCond = bExt.genCmp(yul::CmpPredicate::slt, base, tmpDiv); - evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, panicCond); +Value Lowering::lower(sol::CMulOp op) { + Location loc = op.getLoc(); + solgen::BuilderExt bExt(b, loc); + evm::Builder evmB = evmBuilder(loc); + SmallVector cleaned = getCleaned(op); + Value lhs = cleaned[0], rhs = cleaned[1]; + + auto ty = cast(op.getType()); + Value rawProduct = b.create(loc, lhs, rhs); + Value product = evmB.genCleanup(op.getType(), rawProduct, loc); + auto zero = bExt.genI256Const(0, loc); + if (ty.getWidth() <= 128) { + // overflow/underflow, if product != product_raw + Value panicCond = bExt.genCmp(yul::CmpPredicate::ne, rawProduct, product); + evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, panicCond); + return product; + } + + if (ty.isSigned()) { + // (Copied from the yul codegen) + // underflow, if x < 0 and y == int.min + if (ty.getWidth() == 256) { + auto lhsLtZero = bExt.genCmp(yul::CmpPredicate::slt, lhs, zero); + auto minVal = + bExt.genI256Const(APInt::getSignedMinValue(ty.getWidth()), ty, loc); + auto rhsEqMin = bExt.genCmp(yul::CmpPredicate::eq, rhs, minVal); + evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, + b.create(loc, lhsLtZero, rhsEqMin)); } - // If 4 end - r.setInsertionPointAfter(ifBaseSgtZero); - Value lsb = r.create(loc, exp, one); - Value expIsOdd = bExt.genCmp(yul::CmpPredicate::ne, lsb, zero); - Value pow2 = - r.create(loc, expIsOdd, base, one).getResult(); - Value base2 = r.create(loc, base, base); - Value exp2 = r.create(loc, exp, one); - auto [pow3, base3] = - genExpHelper(getModule(op), r, loc, pow2, base2, exp2, max256); - - r.setInsertionPointToEnd(&ifExpEqOne.getElseRegion().front()); - { - auto zero256 = bExt.genI256Const(0, loc); - Value div = r.create(loc, max256, base3); - Value cmp = bExt.genCmp(yul::CmpPredicate::ugt, pow3, div); - Value cmp2 = bExt.genCmp(yul::CmpPredicate::sgt, pow3, zero256); - Value panicCond = r.create(loc, cmp, cmp2); - evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, panicCond); - } + // over/underflow, if x != 0 and product/x != y + auto lhsNeqZero = bExt.genCmp(yul::CmpPredicate::ne, lhs, zero); + auto quotient = b.create(loc, product, lhs); + auto quotientNeqRhs = bExt.genCmp(yul::CmpPredicate::ne, quotient, rhs); + evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, + b.create(loc, lhsNeqZero, quotientNeqRhs)); + } else { + // over/underflow, if x != 0 and product/x != y + auto lhsNeqZero = bExt.genCmp(yul::CmpPredicate::ne, lhs, zero); + auto quotient = b.create(loc, product, lhs); + auto quotientNeqRhs = bExt.genCmp(yul::CmpPredicate::ne, quotient, rhs); + evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, + b.create(loc, lhsNeqZero, quotientNeqRhs)); + } + return product; +} + +Value Lowering::lower(sol::CDivOp op) { + Location loc = op.getLoc(); + solgen::BuilderExt bExt(b, loc); + evm::Builder evmB = evmBuilder(loc); + SmallVector cleaned = getCleaned(op); + Value lhs = cleaned[0], rhs = cleaned[1]; + + auto ty = cast(op.getType()); + evmB.genDivisionByZeroPanic(rhs, loc); + + if (!ty.isSigned()) + return b.create(loc, lhs, rhs); + + // (Copied from the yul codegen) + // underflow, if x == int.min and y == -1 + auto minVal = + bExt.genI256Const(APInt::getSignedMinValue(ty.getWidth()), ty, loc); + auto lhsEqMin = bExt.genCmp(yul::CmpPredicate::eq, lhs, minVal); + auto minusOne = bExt.genI256Const(-1, loc); + auto rhsEqMinusOne = bExt.genCmp(yul::CmpPredicate::eq, rhs, minusOne); + evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, + b.create(loc, lhsEqMin, rhsEqMinusOne)); + return b.create(loc, lhs, rhs); +} + +// The helper implements: +// +// function checked_exp_helper(power, base, exponent, max) -> power, base { +// for { } gt(exponent, 1) {} +// { +// if gt(base, div(max, base)) { panic_error_0x11() } +// if and(exponent, 1) +// { +// power := mul(power, base) +// } +// base := mul(base, base) +// exponent := shift_right_1_unsigned(exponent) +// } +// } +std::pair Lowering::genExpHelper(Location loc, Value initPow, + Value initBase, Value initExp, + Value maxPow) { + solgen::BuilderExt bExt(b, loc); + evm::Builder evmB = evmBuilder(loc); + + Value one = bExt.genI256Const(1, loc); + SmallVector whileInitVals{initPow, initBase, initExp}; + TypeRange whileArgTypes(whileInitVals); + SmallVector whileBbArgLocs(whileInitVals.size(), loc); + + auto whileOp = b.create(loc, whileArgTypes, whileInitVals); + { + Block *condBlock = &whileOp.getCond().emplaceBlock(); + whileOp.getCond().addArguments(whileArgTypes, whileBbArgLocs); + b.setInsertionPointToStart(condBlock); + Value curPow = condBlock->getArgument(0); + Value curBase = condBlock->getArgument(1); + Value curExp = condBlock->getArgument(2); + Value cmp = bExt.genCmp(yul::CmpPredicate::ugt, curExp, one); + b.create(loc, cmp, ValueRange{curPow, curBase, curExp}); + } + + { + Block *bodyBlock = &whileOp.getBody().emplaceBlock(); + whileOp.getBody().addArguments(whileArgTypes, whileBbArgLocs); + b.setInsertionPointToStart(bodyBlock); + Value curPow = bodyBlock->getArgument(0); + Value curBase = bodyBlock->getArgument(1); + Value curExp = bodyBlock->getArgument(2); { - auto zero256 = bExt.genI256Const(0, loc); - Value div = r.create(loc, min256, base3); - Value cmp = bExt.genCmp(yul::CmpPredicate::slt, pow3, div); - Value cmp2 = bExt.genCmp(yul::CmpPredicate::slt, pow3, zero256); - Value panicCond = r.create(loc, cmp, cmp2); + Value tmpDiv = b.create(loc, maxPow, curBase); + Value panicCond = bExt.genCmp(yul::CmpPredicate::ugt, curBase, tmpDiv); evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, panicCond); } + Value lsb = b.create(loc, curExp, one); + Value zero = bExt.genI256Const(0, loc); + Value expIsOdd = bExt.genCmp(yul::CmpPredicate::ne, lsb, zero); + Value newPow = + b.create( + loc, expIsOdd, b.create(loc, curPow, curBase), curPow) + .getResult(); + Value newBase = b.create(loc, curBase, curBase); + Value newExp = b.create(loc, curExp, one); + b.create(loc, ValueRange{newPow, newBase, newExp}); + } - Value pow4 = r.create(loc, pow3, base3); - r.create(loc, pow4); - // If 3 (ifExpEqOne) end - - r.setInsertionPointToEnd(&ifBaseEqZero.getElseRegion().front()); - r.create(loc, ifExpEqOne.getResult(0)); - // If 2 (ifBaseEqZero) end + return {whileOp.getResult(0), whileOp.getResult(1)}; +} - r.setInsertionPointToEnd(&ifExpEqZero.getElseRegion().front()); - r.create(loc, ifBaseEqZero.getResult(0)); - // If 1 (ifExpEqZero) end +// Implements: +// +// function checked_exp_unsigned(base, exponent, max) -> power { +// if iszero(exponent) { power := 1 leave } +// if iszero(base) { power := 0 leave } +// switch base +// case 1 { power := 1 leave } +// case 2 +// { +// if gt(exponent, 255) { panic_error_0x11() } +// power := exp(2, exponent) +// if gt(power, max) { panic_error_0x11() } +// leave +// } +// if or(and(lt(base, 11), lt(exponent, 78)), +// and(lt(base, 307), lt(exponent, 32))) +// { +// power := exp(base, exponent) +// if gt(power, max) { panic_error_0x11() } +// leave +// } +// power, base := checked_exp_helper(1, base, exponent, max) +// if gt(power, div(max, base)) { panic_error_0x11() } +// power := mul(power, base) +// } +Value Lowering::genExpUnsigned(sol::CExpOp op, Value base, Value exp) { + Location loc = op.getLoc(); + solgen::BuilderExt bExt(b, loc); + evm::Builder evmB = evmBuilder(loc); + + IntegerType i256Ty = b.getIntegerType(256); + auto ty = cast(op.getType()); + Value max256 = + bExt.genI256Const(llvm::APInt::getMaxValue(ty.getWidth()), ty, loc); + Value zero = bExt.genI256Const(0, loc); + Value one = bExt.genI256Const(1, loc); + + auto expEqZero = bExt.genCmp(yul::CmpPredicate::eq, exp, zero); + auto baseEqOne = bExt.genCmp(yul::CmpPredicate::eq, base, one); + auto powOneCond = b.create(loc, expEqZero, baseEqOne); + + // If 1 begin + auto ifExpEqZero = bExt.createIf(i256Ty, powOneCond); + // If 1 then + b.setInsertionPointToStart(&ifExpEqZero.getThenRegion().front()); + b.create(loc, one); + + // If 1 else + b.setInsertionPointToStart(&ifExpEqZero.getElseRegion().front()); + auto baseEqZero = bExt.genCmp(yul::CmpPredicate::eq, base, zero); + + // If 2 begin + auto ifBaseEqZero = bExt.createIf(i256Ty, baseEqZero); + // If 2 then + b.setInsertionPointToStart(&ifBaseEqZero.getThenRegion().front()); + b.create(loc, zero); + + // If 2 else + b.setInsertionPointToStart(&ifBaseEqZero.getElseRegion().front()); + Value two = bExt.genI256Const(2, loc); + auto baseEqTwo = bExt.genCmp(yul::CmpPredicate::eq, base, two); + + // If 3 begin + auto ifBaseEqTwo = bExt.createIf(i256Ty, baseEqTwo); + // If 3 then + b.setInsertionPointToStart(&ifBaseEqTwo.getThenRegion().front()); + { + auto expGt255 = + bExt.genCmp(yul::CmpPredicate::ugt, exp, bExt.genI256Const(255, loc)); + evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, expGt255); + + auto two256 = bExt.genI256Const(2, loc); + Value tmpPow = b.create(loc, two256, exp); + auto panicCond = bExt.genCmp(yul::CmpPredicate::ugt, tmpPow, max256); + evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, panicCond); + + b.create(loc, tmpPow); + } + // If 3 else + b.setInsertionPointToStart(&ifBaseEqTwo.getElseRegion().front()); + auto baseLT11 = + bExt.genCmp(yul::CmpPredicate::ult, base, bExt.genI256Const(11, loc)); + Value baseLT307 = (ty.getWidth() > 8) + ? bExt.genCmp(yul::CmpPredicate::ult, base, + bExt.genI256Const(307, loc)) + : bExt.genBool(true, loc); + + auto expLT78 = + bExt.genCmp(yul::CmpPredicate::ult, exp, bExt.genI256Const(78, loc)); + auto expLT32 = + bExt.genCmp(yul::CmpPredicate::ult, exp, bExt.genI256Const(32, loc)); + + auto viaExpBuitinCond = + b.create(loc, b.create(loc, baseLT11, expLT78), + b.create(loc, baseLT307, expLT32)); + // If 4 begin + auto ifViaExpBuitinCond = bExt.createIf(i256Ty, viaExpBuitinCond); + // If 4 then + b.setInsertionPointToStart(&ifViaExpBuitinCond.getThenRegion().front()); + + { + Value tmpPow = b.create(loc, base, exp); + Value panicCond = bExt.genCmp(yul::CmpPredicate::ugt, tmpPow, max256); + evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, panicCond); + + b.create(loc, tmpPow); + } + // If 4 else + b.setInsertionPointToStart(&ifViaExpBuitinCond.getElseRegion().front()); + auto [pow2, base2] = genExpHelper(loc, one, base, exp, max256); + + { + b.setInsertionPointToEnd(&ifViaExpBuitinCond.getElseRegion().front()); + Value tmpDiv = b.create(loc, max256, base2); + Value panicCond = bExt.genCmp(yul::CmpPredicate::ugt, pow2, tmpDiv); + evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, panicCond); + Value res = b.create(loc, base2, pow2); + b.create(loc, res); + } + // If 4 (ifViaExpBuitinCond) end + b.setInsertionPointToEnd(&ifBaseEqTwo.getElseRegion().front()); + b.create(loc, ifViaExpBuitinCond.getResult(0)); + // If 3 (ifBaseEqTwo) end + + b.setInsertionPointToEnd(&ifBaseEqZero.getElseRegion().front()); + b.create(loc, ifBaseEqTwo.getResult(0)); + // If 2 (ifBaseEqZero) end + + b.setInsertionPointToEnd(&ifExpEqZero.getElseRegion().front()); + b.create(loc, ifBaseEqZero.getResult(0)); + // If 1 (ifExpEqZero) end + + return ifExpEqZero.getResult(0); +} - r.setInsertionPointAfter(ifExpEqZero); +// Implements: +// +// function checked_exp_signed(base, exponent, min, max) -> power { +// switch exponent +// case 0 { power := 1 leave } +// case 1 { power := base leave } +// if iszero(base) { power := 0 leave } +// +// power := 1 +// switch sgt(base, 0) +// case 1 { if gt(base, div(max, base)) { panic_error_0x11() } } +// case 0 { if slt(base, sdiv(max, base)) { panic_error_0x11() } } +// if and(exponent, 1) +// { +// power := base +// } +// base := mul(base, base) +// exponent := shift_right_1_unsigned(exponent) +// +// power, base := checked_exp_helper(power, base, exponent, max) +// +// if and(sgt(power, 0), gt(power, div(max, base))) { panic_error_0x11() } +// if and(slt(power, 0), slt(power, sdiv(min, base))) { panic_error_0x11() +// } power := mul(power, base) +// } +Value Lowering::genExpSigned(sol::CExpOp op, Value base, Value exp) { + Location loc = op.getLoc(); + solgen::BuilderExt bExt(b, loc); + evm::Builder evmB = evmBuilder(loc); + + IntegerType i256Ty = b.getIntegerType(256); + auto ty = cast(op.getType()); + Value zero = bExt.genI256Const(0, loc); + Value one = bExt.genI256Const(1, loc); + Value max256 = + bExt.genI256Const(llvm::APInt::getSignedMaxValue(ty.getWidth()), ty, loc); + Value min256 = + bExt.genI256Const(llvm::APInt::getSignedMinValue(ty.getWidth()), ty, loc); + + auto expEqZero = bExt.genCmp(yul::CmpPredicate::eq, exp, zero); + + // If 1 begin + auto ifExpEqZero = bExt.createIf(i256Ty, expEqZero); + // If 1 then + b.setInsertionPointToStart(&ifExpEqZero.getThenRegion().front()); + b.create(loc, one); + + // If 1 else + b.setInsertionPointToStart(&ifExpEqZero.getElseRegion().front()); + auto baseEqZero = bExt.genCmp(yul::CmpPredicate::eq, base, zero); + + // If 2 begin + auto ifBaseEqZero = bExt.createIf(i256Ty, baseEqZero); + // If 2 then + b.setInsertionPointToStart(&ifBaseEqZero.getThenRegion().front()); + b.create(loc, zero); + + // If 2 else + b.setInsertionPointToStart(&ifBaseEqZero.getElseRegion().front()); + auto expEqOne = bExt.genCmp(yul::CmpPredicate::eq, exp, one); + + // If 3 begin + auto ifExpEqOne = bExt.createIf(i256Ty, expEqOne); + // If 3 then + b.setInsertionPointToStart(&ifExpEqOne.getThenRegion().front()); + b.create(loc, base); + + // If 3 else + b.setInsertionPointToStart(&ifExpEqOne.getElseRegion().front()); + auto baseSgtZero = bExt.genCmp(yul::CmpPredicate::sgt, base, zero); + + // If 4 begin + auto ifBaseSgtZero = bExt.createIf(TypeRange{}, baseSgtZero, + /*withElseRegion=*/true); + // If 4 then + b.setInsertionPointToStart(&ifBaseSgtZero.getThenRegion().front()); + { + Value tmpDiv = b.create(loc, max256, base); + Value panicCond = bExt.genCmp(yul::CmpPredicate::ugt, base, tmpDiv); + evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, panicCond); + } + // If 4 else + b.setInsertionPointToStart(&ifBaseSgtZero.getElseRegion().front()); + { + Value tmpDiv = b.create(loc, max256, base); + Value panicCond = bExt.genCmp(yul::CmpPredicate::slt, base, tmpDiv); + evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, panicCond); + } + // If 4 end + b.setInsertionPointAfter(ifBaseSgtZero); + + Value lsb = b.create(loc, exp, one); + Value expIsOdd = bExt.genCmp(yul::CmpPredicate::ne, lsb, zero); + Value pow2 = + b.create(loc, expIsOdd, base, one).getResult(); + Value base2 = b.create(loc, base, base); + Value exp2 = b.create(loc, exp, one); + auto [pow3, base3] = genExpHelper(loc, pow2, base2, exp2, max256); + + b.setInsertionPointToEnd(&ifExpEqOne.getElseRegion().front()); + { + auto zero256 = bExt.genI256Const(0, loc); + Value div = b.create(loc, max256, base3); + Value cmp = bExt.genCmp(yul::CmpPredicate::ugt, pow3, div); + Value cmp2 = bExt.genCmp(yul::CmpPredicate::sgt, pow3, zero256); + Value panicCond = b.create(loc, cmp, cmp2); + evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, panicCond); + } + { + auto zero256 = bExt.genI256Const(0, loc); + Value div = b.create(loc, min256, base3); + Value cmp = bExt.genCmp(yul::CmpPredicate::slt, pow3, div); + Value cmp2 = bExt.genCmp(yul::CmpPredicate::slt, pow3, zero256); + Value panicCond = b.create(loc, cmp, cmp2); + evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, panicCond); + } + + Value pow4 = b.create(loc, pow3, base3); + b.create(loc, pow4); + // If 3 (ifExpEqOne) end + + b.setInsertionPointToEnd(&ifBaseEqZero.getElseRegion().front()); + b.create(loc, ifExpEqOne.getResult(0)); + // If 2 (ifBaseEqZero) end + + b.setInsertionPointToEnd(&ifExpEqZero.getElseRegion().front()); + b.create(loc, ifBaseEqZero.getResult(0)); + // If 1 (ifExpEqZero) end + + return ifExpEqZero.getResult(0); +} - return ifExpEqZero.getResult(0); - } +Value Lowering::lower(sol::CExpOp op) { + SmallVector cleaned = getCleaned(op); + Value base = cleaned[0], exp = cleaned[1]; + if (cast(op.getType()).isSigned()) + return genExpSigned(op, base, exp); + return genExpUnsigned(op, base, exp); +} - LogicalResult rewriteCleaned(sol::CExpOp op, ConversionPatternRewriter &r, - Value base, Value exp) const { - auto ty = cast(op.getType()); - Value res = ty.isSigned() ? expSigned(op, r, base, exp) - : expUnsigned(op, r, base, exp); - r.replaceOp(op, res); +//===----------------------------------------------------------------------===// +// Casts +//===----------------------------------------------------------------------===// - return success(); - } -}; +Value Lowering::lower(sol::CastOp op) { + Location loc = op.getLoc(); + auto inpTyWidth = cast(op.getInp().getType()).getWidth(); + auto outTyWidth = cast(op.getType()).getWidth(); -} // namespace + Value val = lowered(op.getInp()); + // old codegen begin + // A widening cast cleans its source width; a narrowing one is a typed view, + // cleaned where it is consumed: comparisons, ABI and stores. + // (Via-IR cleans in both directions). + if (outTyWidth > inpTyWidth) + val = evmBuilder(loc).genCleanup(op.getInp().getType(), val, loc); + // old codegen end + return val; +} -template -static Value getCryptoHashLowering(OpT op, uint32_t preCompieAddr, - typename OpT::Adaptor adaptor, - ConversionPatternRewriter &r, - evm::StringLiteralPool &literals) { +Value Lowering::lower(sol::AddressCastOp op) { Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - evm::Builder evmB(getModule(op), r, loc, literals); - - sol::DataLocation dataLoc = sol::DataLocation::Memory; + solgen::BuilderExt bExt(b, loc); + Type inpTy = op.getInp().getType(); + Type outTy = op.getType(); + Value inp = lowered(op.getInp()); + + if (isa(inpTy) || isa(outTy)) { + // bytes20 -> address + if (auto inpBytesTy = dyn_cast(inpTy)) { + assert(inpBytesTy.getSize() == 20 && + "AddressCastOp only supports bytes20"); + assert(isa(outTy)); + return b.create(loc, inp, bExt.genI256Const(96)); + } - Value zero = bExt.genI256Const(0); - Value retSize = bExt.genI256Const(32); - Value dataLen, dataSlot; - if (isa(op.getData().getType())) { - StringRef bytes = - cast(op.getData().getDefiningOp()).getValue(); - // The precompile reads the bytes only during the call, so they skip the - // allocation a memory literal gets: they are written at the free memory - // pointer, and the pointer does not move. - dataSlot = evmB.genFreePtr(); - evmB.genStringLiteralData(bytes, dataSlot); - dataLen = bExt.genI256Const(bytes.size()); - } else { - assert(sol::getDataLocation(op.getData().getType()) == dataLoc); - dataLen = evmB.genLoad(adaptor.getData(), dataLoc); - dataSlot = evmB.genDataAddrPtr(adaptor.getData(), dataLoc); + // address -> bytes20 + assert(cast(outTy).getSize() == 20 && + "AddressCastOp only supports bytes20"); + assert(isa(inpTy)); + return b.create(loc, inp, bExt.genI256Const(96)); } - Value gas = r.create(loc); - // Hashing functions store their result in scratch space (0x00–0x3f). - mlir::Value status = - r.create(loc, gas, - /*address=*/bExt.genI256Const(preCompieAddr), - /*inpOffset=*/dataSlot, dataLen, - /*outOffset=*/zero, /*outSize=*/retSize); - - auto statusIsZero = bExt.genCmp(yul::CmpPredicate::eq, status, zero); - evmB.genForwardingRevert(statusIsZero); - - auto res = evmB.genLoad(zero, dataLoc); - // For ripemd160 the result must be shifted left by 96 bits. - if constexpr (std::is_same_v, sol::Ripemd160Op>) - res = r.create(loc, bExt.genI256Const(96), res); - - return res; + // old codegen begin + // (Via-IR adds cleanup helpers around address casts), + if (isa(inpTy) || isa(outTy)) { + // uint160 -> address + if (isa(inpTy)) { + [[maybe_unused]] auto inpIntTy = cast(inpTy); + assert(inpIntTy.getWidth() == 160 && !inpIntTy.isSigned() && + "AddressCastOp only supports uint160 -> address"); + assert(isa(outTy)); + } else { + // address -> uint160 + [[maybe_unused]] auto outIntTy = cast(outTy); + assert(isa(inpTy) && outIntTy.getWidth() == 160 && + !outIntTy.isSigned() && + "AddressCastOp only supports address -> uint160"); + } + return inp; + } + + if (isa(inpTy) && isa(outTy)) + // address -> address (payable <-> non-payable) + assert(cast(inpTy).getPayable() != + cast(outTy).getPayable() && + "AddressCastOp only supports payable <-> non-payable address casts"); + else if (!(isa(inpTy) && isa(outTy)) && + !(isa(inpTy) && isa(outTy))) + llvm_unreachable("Unexpected types for AddressCastOp"); + return inp; + // old codegen end } -template -static Value genYulModOp(ModuleOp module, ConversionPatternRewriter &r, - Location loc, Value x, Value y, Value mod) { - evm::Builder evmB(module, r, loc); +// old codegen begin +// (Via-IR adds cleanup helpers around contract casts). +Value Lowering::lower(sol::ContractCastOp op) { return lowered(op.getInp()); } +// old codegen end - evmB.genDivisionByZeroPanic(mod, loc); - return r.create(loc, x, y, mod); +// An int -> enum cast cleans the input and the output. An enum -> int cast +// cleans only the input: an enum value is always in the valid range and thus +// also valid as an int value. +Value Lowering::lower(sol::EnumCastOp op) { + Value val = getCleaned(op)[0]; + if (isa(op.getType())) + val = evmBuilder(op.getLoc()).genCleanup(op.getType(), val, op.getLoc()); + return val; } -namespace { - -struct ShlOpLowering - : public CleanedOperandsLowering { - using Base = CleanedOperandsLowering; - using Base::Base; +Value Lowering::lower(sol::BytesCastOp op) { + Location loc = op.getLoc(); + solgen::BuilderExt bExt(b, loc); + Type inpTy = op.getInp().getType(); + Type outTy = op.getType(); + Value inp = lowered(op.getInp()); + + const bool isByteToBytes1 = isa(inpTy) && + isa(outTy) && + sol::getNumBytes(outTy) == 1; + const bool isBytes1ToByte = isa(inpTy) && + sol::getNumBytes(inpTy) == 1 && + isa(outTy); + // byte <-> bytes1 is a no-op. + if (isByteToBytes1 || isBytes1ToByte) + return inp; + + if (sol::isBytesLikeType(inpTy)) { + unsigned inpBytesSize = sol::getNumBytes(inpTy); + if (sol::isBytesLikeType(outTy)) { + // old codegen begin + // bytesN -> bytesM shortening is intentionally dirty for old-codegen + // stack-slot parity. Widening must clear bytes beyond the source width. + // (Via-IR cleans in both directions). + if (inpBytesSize < sol::getNumBytes(outTy)) + return evmBuilder(loc).genCleanup(inpTy, inp, loc); + return inp; + // old codegen end + } - LogicalResult rewriteCleaned(sol::ShlOp op, ConversionPatternRewriter &r, - Value val, Value shift) const { - r.replaceOpWithNewOp(op, shift, val); - return success(); + // Bytes to int + auto shiftAmt = bExt.genI256Const(256 - (8 * inpBytesSize)); + return b.create(loc, inp, shiftAmt); } -}; -struct ShrOpLowering - : public CleanedOperandsLowering { - using Base = CleanedOperandsLowering; - using Base::Base; - - LogicalResult rewriteCleaned(sol::ShrOp op, ConversionPatternRewriter &r, - Value val, Value shift) const { - if (auto ty = dyn_cast(op.getType())) - if (ty.isSigned()) { - r.replaceOpWithNewOp(op, shift, val); - return success(); - } - r.replaceOpWithNewOp(op, shift, val); - return success(); - } -}; - -struct CAddOpLowering - : public CleanedOperandsLowering { - using Base = CleanedOperandsLowering; - using Base::Base; - - LogicalResult rewriteCleaned(sol::CAddOp op, ConversionPatternRewriter &r, - Value lhs, Value rhs) const { - Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - evm::Builder evmB(getModule(op), r, loc); - - auto ty = cast(op.getType()); - Value sum = r.create(loc, lhs, rhs); - - if (ty.getWidth() < 256) { - if (ty.isSigned()) { - // overflow, if sum > max - // underflow, if sum < min - Value max = bExt.genI256Const( - APInt::getSignedMaxValue(ty.getWidth()).sext(256), loc); - Value overMax = bExt.genCmp(yul::CmpPredicate::sgt, sum, max); - Value min = bExt.genI256Const( - APInt::getSignedMinValue(ty.getWidth()).sext(256), loc); - Value underMin = bExt.genCmp(yul::CmpPredicate::slt, sum, min); - evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, - r.create(loc, overMax, underMin)); - } else { - // overflow, if sum > max - Value max = - bExt.genI256Const(APInt::getMaxValue(ty.getWidth()).zext(256), loc); - evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, - bExt.genCmp(yul::CmpPredicate::ugt, sum, max)); - } - r.replaceOp(op, sum); - return success(); - } - - if (ty.isSigned()) { - // (Copied from the yul codegen) - // overflow, if y >= 0 and sum < x - // underflow, if y < 0 and sum >= x - // - // We compare rhs with zero since the canonicalizer could make the rhs a - // constant which would enable the arith dialect to optimize away the - // comparison. - - auto zero = bExt.genI256Const(0, loc); - - // Generate the overflow condition. - auto rhsGtEqZero = bExt.genCmp(yul::CmpPredicate::sge, rhs, zero); - auto sumLtLhs = bExt.genCmp(yul::CmpPredicate::slt, sum, lhs); - auto overflowCond = r.create(loc, rhsGtEqZero, sumLtLhs); - - // Generate the underflow condition. - auto rhsLtZero = bExt.genCmp(yul::CmpPredicate::slt, rhs, zero); - auto sumGtEqLhs = bExt.genCmp(yul::CmpPredicate::sge, sum, lhs); - auto underflowCond = r.create(loc, rhsLtZero, sumGtEqLhs); - - evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, - r.create(loc, overflowCond, underflowCond)); - - // Unsigned case - } else { - evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, - bExt.genCmp(yul::CmpPredicate::ugt, lhs, sum)); - } - - r.replaceOp(op, sum); - return success(); - } -}; - -struct CSubOpLowering - : public CleanedOperandsLowering { - using Base = CleanedOperandsLowering; - using Base::Base; - - LogicalResult rewriteCleaned(sol::CSubOp op, ConversionPatternRewriter &r, - Value lhs, Value rhs) const { - Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - evm::Builder evmB(getModule(op), r, loc); - - auto ty = cast(op.getType()); - Value diff = r.create(loc, lhs, rhs); - - if (ty.getWidth() < 256) { - if (ty.isSigned()) { - // overflow, if diff > max - // underflow, if diff < min - Value max = bExt.genI256Const( - APInt::getSignedMaxValue(ty.getWidth()).sext(256), loc); - Value overMax = bExt.genCmp(yul::CmpPredicate::sgt, diff, max); - Value min = bExt.genI256Const( - APInt::getSignedMinValue(ty.getWidth()).sext(256), loc); - Value underMin = bExt.genCmp(yul::CmpPredicate::slt, diff, min); - evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, - r.create(loc, overMax, underMin)); - } else { - // underflow, if diff > max - Value max = - bExt.genI256Const(APInt::getMaxValue(ty.getWidth()).zext(256), loc); - evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, - bExt.genCmp(yul::CmpPredicate::ugt, diff, max)); - } - r.replaceOp(op, diff); - return success(); - } - - if (ty.isSigned()) { - // (Copied from the yul codegen) - // underflow, if y >= 0 and diff > x - // overflow, if y < 0 and diff < x - - auto zero = bExt.genI256Const(0, loc); - - // Generate the overflow condition. - auto rhsGtEqZero = bExt.genCmp(yul::CmpPredicate::sge, rhs, zero); - auto diffGtLhs = bExt.genCmp(yul::CmpPredicate::sgt, diff, lhs); - auto overflowCond = r.create(loc, rhsGtEqZero, diffGtLhs); - - // Generate the underflow condition. - auto rhsLtZero = bExt.genCmp(yul::CmpPredicate::slt, rhs, zero); - auto diffLtRhs = bExt.genCmp(yul::CmpPredicate::slt, diff, lhs); - auto underflowCond = r.create(loc, rhsLtZero, diffLtRhs); - - evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, - r.create(loc, overflowCond, underflowCond)); - - // Unsigned case - } else { - evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, - bExt.genCmp(yul::CmpPredicate::ugt, diff, lhs)); - } - - r.replaceOp(op, diff); - return success(); - } -}; - -struct CMulOpLowering - : public CleanedOperandsLowering { - using Base = CleanedOperandsLowering; - using Base::Base; - - LogicalResult rewriteCleaned(sol::CMulOp op, ConversionPatternRewriter &r, - Value lhs, Value rhs) const { - Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - evm::Builder evmB(getModule(op), r, loc); - - auto ty = cast(op.getType()); - - Value rawProduct = r.create(loc, lhs, rhs); - Value product = evmB.genCleanup(op.getType(), rawProduct, loc); - auto zero = bExt.genI256Const(0, loc); - if (ty.getWidth() <= 128) { - // overflow/underflow, if product != product_raw - Value panicCond = bExt.genCmp(yul::CmpPredicate::ne, rawProduct, product); - evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, panicCond); - r.replaceOp(op, product); - return success(); - } - - if (ty.isSigned()) { - // (Copied from the yul codegen) - // underflow, if x < 0 and y == int.min - if (ty.getWidth() == 256) { - auto lhsLtZero = bExt.genCmp(yul::CmpPredicate::slt, lhs, zero); - auto minVal = bExt.genI256Const( - APInt::getSignedMinValue(ty.getWidth()).sext(256), loc); - auto rhsEqMin = bExt.genCmp(yul::CmpPredicate::eq, rhs, minVal); - evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, - r.create(loc, lhsLtZero, rhsEqMin)); - } - - // over/underflow, if x != 0 and product/x != y - auto lhsNeqZero = bExt.genCmp(yul::CmpPredicate::ne, lhs, zero); - auto quotient = r.create(loc, product, lhs); - auto quotientNeqRhs = bExt.genCmp(yul::CmpPredicate::ne, quotient, rhs); - evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, - r.create(loc, lhsNeqZero, quotientNeqRhs)); - - // Unsigned case - } else { - // over/underflow, if x != 0 and product/x != y - auto lhsNeqZero = bExt.genCmp(yul::CmpPredicate::ne, lhs, zero); - auto quotient = r.create(loc, product, lhs); - auto quotientNeqRhs = bExt.genCmp(yul::CmpPredicate::ne, quotient, rhs); - evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, - r.create(loc, lhsNeqZero, quotientNeqRhs)); - } - - r.replaceOp(op, product); - return success(); - } -}; - -struct AddModOpLowering - : public CleanedOperandsLowering { - using Base = CleanedOperandsLowering; - using Base::Base; - - LogicalResult rewriteCleaned(sol::AddModOp op, ConversionPatternRewriter &r, - Value x, Value y, Value mod) const { - Location loc = op.getLoc(); - Value result = genYulModOp(getModule(op), r, loc, x, y, mod); - r.replaceOp(op, result); - return success(); - } -}; - -struct MulModOpLowering - : public CleanedOperandsLowering { - using Base = CleanedOperandsLowering; - using Base::Base; - - LogicalResult rewriteCleaned(sol::MulModOp op, ConversionPatternRewriter &r, - Value x, Value y, Value mod) const { - Location loc = op.getLoc(); - Value result = genYulModOp(getModule(op), r, loc, x, y, mod); - r.replaceOp(op, result); - return success(); - } -}; - -struct CDivOpLowering - : public CleanedOperandsLowering { - using Base = CleanedOperandsLowering; - using Base::Base; - - LogicalResult rewriteCleaned(sol::CDivOp op, ConversionPatternRewriter &r, - Value lhs, Value rhs) const { - Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - evm::Builder evmB(getModule(op), r, loc); - - auto ty = cast(op.getType()); - evmB.genDivisionByZeroPanic(rhs, loc); - - if (ty.isSigned()) { - // (Copied from the yul codegen) - // underflow, if x == int.min and y == -1 - auto minVal = bExt.genI256Const( - APInt::getSignedMinValue(ty.getWidth()).sext(256), loc); - auto lhsEqMin = bExt.genCmp(yul::CmpPredicate::eq, lhs, minVal); - auto minusOne = bExt.genI256Const(-1, loc); - auto rhsEqMinusOne = bExt.genCmp(yul::CmpPredicate::eq, rhs, minusOne); - evmB.genPanic(mlir::evm::PanicCode::UnderOverflow, - r.create(loc, lhsEqMin, rhsEqMinusOne)); - r.replaceOpWithNewOp(op, lhs, rhs); - } else { - r.replaceOpWithNewOp(op, lhs, rhs); - } - - return success(); - } -}; - -struct Keccak256OpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::Keccak256Op op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - evm::Builder evmB(getModule(op), r, loc); - - Type ty = op.getAddr().getType(); - sol::DataLocation dataLoc = sol::getDataLocation(ty); - - Value dataLen = evmB.genLoad(adaptor.getAddr(), dataLoc); - auto dataSlot = evmB.genDataAddrPtr(adaptor.getAddr(), ty); - r.replaceOpWithNewOp(op, dataSlot, dataLen); - - return success(); - } -}; - -struct Erc7201OpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::Erc7201Op op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - evm::Builder evmB(getModule(op), r, loc); - - Type ty = op.getId().getType(); - sol::DataLocation dataLoc = sol::getDataLocation(ty); - - Value dataLen = evmB.genLoad(adaptor.getId(), dataLoc); - auto dataSlot = evmB.genDataAddrPtr(adaptor.getId(), ty); - Value inner = r.create(loc, dataSlot, dataLen); - Value decremented = r.create(loc, inner, bExt.genI256Const(1)); - // The outer hash input is a single word, so it fits the scratch space. - Value zero = bExt.genI256Const(0); - r.create(loc, zero, decremented); - Value outer = r.create(loc, zero, bExt.genI256Const(32)); - Value mask = r.create(loc, bExt.genI256Const(0xff)); - r.replaceOpWithNewOp(op, outer, mask); - - return success(); - } -}; - -struct Sha256OpLowering : public OpConversionPattern { - Sha256OpLowering(TypeConverter &tyConv, MLIRContext *ctx, - evm::StringLiteralPool &literals) - : OpConversionPattern(tyConv, ctx), literals(literals) {} - - LogicalResult matchAndRewrite(sol::Sha256Op op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - r.replaceOp(op, getCryptoHashLowering(op, /*preCompieAddr=*/2, adaptor, r, - literals)); - - return success(); - } - - evm::StringLiteralPool &literals; -}; - -struct Ripemd160OpLowering : public OpConversionPattern { - Ripemd160OpLowering(TypeConverter &tyConv, MLIRContext *ctx, - evm::StringLiteralPool &literals) - : OpConversionPattern(tyConv, ctx), literals(literals) {} - - LogicalResult matchAndRewrite(sol::Ripemd160Op op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - r.replaceOp(op, getCryptoHashLowering(op, /*preCompieAddr=*/3, adaptor, r, - literals)); - - return success(); - } - - evm::StringLiteralPool &literals; -}; - -struct EcrecoverOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::EcrecoverOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - evm::Builder evmB(getModule(op), r, loc); - - Value zero = bExt.genI256Const(0); - Value retSize = bExt.genI256Const(32); - Value freePtr = evmB.genFreePtr(); - // Since the size of the encoded arguments is known in advance, - // we ignore the return value of genABIEncoding. - Value paramsSize = bExt.genI256Const(128); - // The precompile reads the input only during the call, so the free pointer - // does not move. - evmB.genABIEncoding(op.getOperandTypes(), adaptor.getOperands(), freePtr); - - // Hashing functions store their result in scratch space (0x00–0x3f). - Value gas = r.create(loc); - // The output area must be cleared before the call: a failing ecrecover - // cannot be detected from the status (the precompile succeeds with empty - // returndata, leaving the output area untouched), so this zero is what - // the caller reads back as the documented address(0) failure result. - // Scratch is dirty in general, so we need to clean it. - r.create(loc, zero, zero); - mlir::Value status = - r.create(loc, gas, - /*address=*/bExt.genI256Const(1), - /*inpOffset=*/freePtr, paramsSize, - /*outOffset=*/zero, /*outSize=*/retSize); - - auto statusIsZero = bExt.genCmp(yul::CmpPredicate::eq, status, zero); - evmB.genForwardingRevert(statusIsZero); - auto res = evmB.genLoad(zero, sol::DataLocation::Memory); - r.replaceOp(op, res); - - return success(); - } -}; - -struct CmpOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - yul::CmpPredicate getSignlessPred(sol::CmpPredicate pred, - bool isSigned) const { - // Sign insensitive predicates. - switch (pred) { - case sol::CmpPredicate::eq: - return yul::CmpPredicate::eq; - case sol::CmpPredicate::ne: - return yul::CmpPredicate::ne; - default: - break; - } - - // Sign sensitive predicates. - if (isSigned) { - switch (pred) { - case sol::CmpPredicate::lt: - return yul::CmpPredicate::slt; - case sol::CmpPredicate::le: - return yul::CmpPredicate::sle; - case sol::CmpPredicate::gt: - return yul::CmpPredicate::sgt; - case sol::CmpPredicate::ge: - return yul::CmpPredicate::sge; - default: - break; - } - } else { - switch (pred) { - case sol::CmpPredicate::lt: - return yul::CmpPredicate::ult; - case sol::CmpPredicate::le: - return yul::CmpPredicate::ule; - case sol::CmpPredicate::gt: - return yul::CmpPredicate::ugt; - case sol::CmpPredicate::ge: - return yul::CmpPredicate::uge; - default: - break; - } - } - llvm_unreachable("Invalid predicate"); - } - - LogicalResult matchAndRewrite(sol::CmpOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - evm::Builder evmB(getModule(op), r, loc); - bool isSigned = false; - if (auto intTy = dyn_cast(op.getLhs().getType())) - isSigned = intTy.isSigned(); - - yul::CmpPredicate signlessPred = - getSignlessPred(op.getPredicate(), isSigned); - if (isa(op.getLhs().getType())) { - assert((signlessPred == yul::CmpPredicate::eq || - signlessPred == yul::CmpPredicate::ne) && - "Illegal compare mode"); - auto i256Ty = r.getIntegerType(256); - auto genLaneCmp = [&](int64_t idx, unsigned numBits) { - Value mask = bExt.genI256Const(APInt::getLowBitsSet(256, numBits)); - Value lhsLane = r.create( - loc, i256Ty, adaptor.getLhs(), r.getDenseI64ArrayAttr({idx})); - Value rhsLane = r.create( - loc, i256Ty, adaptor.getRhs(), r.getDenseI64ArrayAttr({idx})); - return bExt.genCmp(signlessPred, - r.create(loc, lhsLane, mask), - r.create(loc, rhsLane, mask)); - }; - Value addrCmp = genLaneCmp(0, 160); - Value selCmp = genLaneCmp(1, 32); - Value combined = - signlessPred == yul::CmpPredicate::eq - ? r.create(loc, addrCmp, selCmp).getResult() - : r.create(loc, addrCmp, selCmp).getResult(); - r.replaceOp(op, combined); - return success(); - } - Value lhs = evmB.genCleanup(op.getLhs().getType(), adaptor.getLhs(), loc); - Value rhs = evmB.genCleanup(op.getRhs().getType(), adaptor.getRhs(), loc); - r.replaceOp(op, bExt.genCmp(signlessPred, lhs, rhs)); - return success(); - } -}; - -struct AllocaOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::AllocaOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - mlir::solgen::BuilderExt bExt(r, op.getLoc()); - - auto ptrTy = cast(op.getAllocType()); - Type convertedEltTy = - getTypeConverter()->convertType(ptrTy.getPointeeType()); - - r.replaceOpWithNewOp( - op, LLVM::LLVMPointerType::get(r.getContext()), convertedEltTy, - bExt.genI256Const(1)); - return success(); - } -}; - -struct MallocOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::MallocOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - evm::Builder evmB(getModule(op), r, op.getLoc()); - Type sizeTy = op.getSize() ? op.getSize().getType() : Type{}; - r.replaceOp(op, evmB.genMemAlloc(op.getType(), op.getZeroInit(), {}, - adaptor.getSize(), sizeTy)); - return success(); - } -}; - -struct ArrayLitOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::ArrayLitOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - evm::Builder evmB(getModule(op), r, loc); - r.replaceOp(op, evmB.genMemAlloc(op.getType(), false, adaptor.getIns(), {}, - Type{})); - return success(); - } -}; - -struct StringLitOpLowering : public OpConversionPattern { - StringLitOpLowering(TypeConverter &tyConv, MLIRContext *ctx, - evm::StringLiteralPool &literals) - : OpConversionPattern(tyConv, ctx), literals(literals) {} - - LogicalResult matchAndRewrite(sol::StringLitOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - - Location loc = op.getLoc(); - auto mod = getModule(op); - evm::Builder evmB(mod, r, loc, literals); - mlir::solgen::BuilderExt bExt(r, op.getLoc()); - - StringRef lit = adaptor.getValue(); - - size_t roundedLen = llvm::alignTo(lit.size(), 32u); - Value allocPtr = evmB.genMemAlloc(bExt.genI256Const(32 + roundedLen), loc); - r.create(loc, allocPtr, bExt.genI256Const(lit.size())); - evmB.genStringLiteralData( - lit, evmB.genDataAddrPtr(allocPtr, sol::DataLocation::Memory, loc), - loc); - - r.replaceOp(op, allocPtr); - return success(); - } - - evm::StringLiteralPool &literals; -}; - -struct ConstantStringOpLowering - : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::ConstantStringOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - // Its consumers, lowered before it, wrote its bytes from its attribute. - r.eraseOp(op); - return success(); - } -}; - -struct ConcatOpLowering : public OpConversionPattern { - ConcatOpLowering(TypeConverter &tyConv, MLIRContext *ctx, - evm::StringLiteralPool &literals) - : OpConversionPattern(tyConv, ctx), literals(literals) {} - - LogicalResult matchAndRewrite(sol::ConcatOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - evm::Builder evmB(getModule(op), r, loc, literals); - mlir::solgen::BuilderExt bExt(r, loc); - - Value freePtr = evmB.genFreePtr(); - Value dstStart = r.create(loc, freePtr, bExt.genI256Const(32)); - - Value currDst = dstStart; - for (auto [origSrc, src] : llvm::zip(op.getArgs(), adaptor.getArgs())) { - Type ty = origSrc.getType(); - if (isa(ty)) { - StringRef bytes = - cast(origSrc.getDefiningOp()).getValue(); - evmB.genStringLiteralData(bytes, currDst, loc); - currDst = - r.create(loc, currDst, bExt.genI256Const(bytes.size())); - continue; - } - - if (auto bytesTy = dyn_cast(ty)) { - // Cleanup the src before copying to memory to ensure that any - // padding bytes are zeroed out. - r.create(loc, currDst, evmB.genCleanup(ty, src, loc)); - Value length = bExt.genI256Const(bytesTy.getSize()); - currDst = r.create(loc, currDst, length); - continue; - } - - assert(isa(ty)); - Value length = evmB.genCopyStringDataToMemory(src, ty, currDst, loc); - currDst = r.create(loc, currDst, length); - } - Value dataSize = r.create(loc, currDst, dstStart); - r.create(loc, freePtr, dataSize); - evmB.genFreePtrStore(currDst); - r.replaceOp(op, freePtr); - - return success(); - } - - evm::StringLiteralPool &literals; -}; - -struct GetCallDataOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::GetCallDataOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - mlir::solgen::BuilderExt bExt(r, op.getLoc()); - Value zero = bExt.genI256Const(0); - if (auto strTy = dyn_cast(op.getType())) { - if (strTy.getDataLocation() == sol::DataLocation::CallData) { - Value callDataSz = r.create(op.getLoc()); - r.replaceOp(op, bExt.genLLVMStruct({zero, callDataSz})); - return success(); - } - } - r.replaceOp(op, zero); - return success(); - } -}; - -struct DefaultCallDataOpLowering - : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::DefaultCallDataOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - mlir::solgen::BuilderExt bExt(r, op.getLoc()); - Value zero = bExt.genI256Const(0); - - Value callDataSz = r.create(op.getLoc()); - - // Fat pointer types get an empty slice: {calldatasize(), 0}. - Type convertedTy = getTypeConverter()->convertType(op.getType()); - if (isa(convertedTy)) { - r.replaceOp(op, bExt.genLLVMStruct({callDataSz, zero})); - return success(); - } - // Scalar calldata types (e.g. structs) default to calldatasize() - - // an offset past valid calldata. - r.replaceOp(op, callDataSz); - return success(); - } -}; - -struct DefaultStorageOpLowering - : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::DefaultStorageOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - mlir::solgen::BuilderExt bExt(r, op.getLoc()); - // Storage and transient aggregate "pointers" are slot numbers. Slot 0 is - // the sentinel default (analogous to calldatasize() for calldata). - r.replaceOp(op, bExt.genI256Const(0)); - return success(); - } -}; - -struct PushOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::PushOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - evm::Builder evmB(getModule(op), r, loc); - mlir::solgen::BuilderExt bExt(r, loc); - - Type inpTy = op.getInp().getType(); - if (isa(inpTy)) { - // The zero push of a string is shared by every site. The result is - // the legalized {slot, byte offset} address of the new byte. - Type resTy = getTypeConverter()->convertType(op.getType()); - r.replaceOp(op, evmB.genHelperCall( - evm::helpersym::pushStorageString(/*zero=*/true), - {adaptor.getInp()}, resTy, - [&](ValueRange args, Location bodyLoc) { - return evmB.genPushVoidToString(args[0], bodyLoc); - }, - loc)); - return success(); - } - - auto arrTy = cast(inpTy); - - // The push body (length update, data slot hashing, packed addressing) is - // emitted once per array type as a helper taking the array slot and - // returning the address of the new element. The result is the legalized - // element pointer: a {slot, byte offset} struct for a packed element, one - // word otherwise. - Type resTy = getTypeConverter()->convertType(op.getType()); - r.replaceOp(op, - evmB.genHelperCall( - evm::helpersym::pushStorageArray(arrTy), {adaptor.getInp()}, - resTy, [&](ValueRange args, Location bodyLoc) { - Value slot = args[0]; - Value oldSize = r.create(bodyLoc, slot); - Value newSize = r.create( - bodyLoc, oldSize, bExt.genI256Const(1, bodyLoc)); - r.create(bodyLoc, slot, newSize); - Value dataSlot = evmB.genDataAddrPtr( - slot, sol::DataLocation::Storage, bodyLoc); - return evmB.genStorageEltAddr(dataSlot, oldSize, arrTy, - bodyLoc); - })); - return success(); - } -}; - -struct PushStringOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::PushStringOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - evm::Builder evmB(getModule(op), r, loc); - - Type inpValueTy = op.getValue().getType(); - Value val = adaptor.getValue(); - - Value byte = - isa(inpValueTy) - ? r.create(loc, val, bExt.genI256Const(248)) - .getResult() - : val; - // The push is shared by every site. It takes the string slot and the - // byte, right-aligned. - evmB.genHelperCall( - evm::helpersym::pushStorageString(/*zero=*/false), - {adaptor.getAddr(), byte}, - [&](ValueRange args, Location bodyLoc) { - evmB.genPushToString(args[0], args[1], bodyLoc); - }, - loc); - r.eraseOp(op); - - return success(); - } -}; - -struct PopOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::PopOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - evm::Builder evmB(getModule(op), r, loc); - mlir::solgen::BuilderExt bExt(r, loc); - - Type inpTy = op.getInp().getType(); - Value slot = adaptor.getInp(); - - if (isa(inpTy)) { - // The string pop (length decoding, empty panic, clear of the removed - // byte, length update) is shared by every site. - evmB.genHelperCall( - evm::helpersym::popStorageString(), {slot}, - [&](ValueRange args, Location bodyLoc) { - Value slot = args[0]; - Value data = - evmB.genLoad(slot, sol::DataLocation::Storage, bodyLoc); - Value oldSize = evmB.genStorageStringLength(data, bodyLoc); - Value panicCond = - bExt.genCmp(yul::CmpPredicate::eq, oldSize, - bExt.genI256Const(0, bodyLoc), bodyLoc); - evmB.genPanic(mlir::evm::PanicCode::EmptyArrayPop, panicCond, - bodyLoc); - evmB.genPopString(slot, data, oldSize, bodyLoc); - }, - loc); - r.eraseOp(op); - return success(); - } - auto arrTy = cast(inpTy); - // The array pop (length load, empty-array panic, clear of the removed - // element, length update) is emitted once per array type as a helper - // taking the array slot. - evmB.genHelperCall( - evm::helpersym::popStorageArray(arrTy), {slot}, - [&](ValueRange args, Location bodyLoc) { - Value slot = args[0]; - Value oldSize = - evmB.genLoad(slot, sol::DataLocation::Storage, bodyLoc); - Value panicCond = bExt.genCmp(yul::CmpPredicate::eq, oldSize, - bExt.genI256Const(0, bodyLoc), bodyLoc); - evmB.genPanic(mlir::evm::PanicCode::EmptyArrayPop, panicCond, - bodyLoc); - Value newSize = r.create(bodyLoc, oldSize, - bExt.genI256Const(1, bodyLoc)); - // Yul reference order. genClearStorageArrayTail handles both packed - // types and slot-aligned types (including multi-slot structs and - // nested dynamic arrays). - // isDecrement=true: newSize = oldSize-1, so the range is always - // non-empty and contains exactly one element. The range guard and - // loop are omitted. - evmB.genClearStorageArrayTail(slot, arrTy, newSize, oldSize, - /*isDecrement=*/true, bodyLoc); - // Write the decremented length after clearing the removed element. - r.create(bodyLoc, slot, newSize); - }); - - r.eraseOp(op); - return success(); - } -}; - -struct AddrOfOpLowering : public OpRewritePattern { - using OpRewritePattern::OpRewritePattern; - - LogicalResult matchAndRewrite(sol::AddrOfOp op, - PatternRewriter &r) const override { - mlir::solgen::BuilderExt bExt(r, op.getLoc()); - - auto parentContract = op->getParentOfType(); - auto *sym = parentContract.lookupSymbol(op.getVar()); - assert(sym); - if (auto stateVarOp = dyn_cast(sym)) { - Value slot = bExt.genI256Const(stateVarOp.getSlot()); - if (sol::canBePacked(stateVarOp.getType())) { - Value offset = bExt.genI256Const(stateVarOp.getByteOffset()); - r.replaceOp(op, bExt.genLLVMStruct({slot, offset})); - } else { - r.replaceOp(op, slot); - } - return success(); - } - auto immOp = cast(sym); - r.replaceOp(op, bExt.genI256Const(evm::getImmutableAddr(immOp))); - return success(); - } -}; - -struct GepOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::GepOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - evm::Builder evmB(getModule(op), r, loc); - - Type baseAddrTy = op.getBaseAddr().getType(); - Value remappedBaseAddr = adaptor.getBaseAddr(); - Value idx = adaptor.getIdx(); - Value cleanedIdx; - auto getCleanedIdx = [&]() -> Value { - if (!cleanedIdx) - cleanedIdx = evmB.genCleanup(op.getIdx().getType(), idx, loc); - return cleanedIdx; - }; - auto genBoundsCheck = [&](Value cond, - std::optional locArg = std::nullopt) { - if (op.getNoPanicBounds()) - evmB.genRevert(cond, locArg); - else - evmB.genPanic(mlir::evm::PanicCode::ArrayOutOfBounds, cond, locArg); - }; - auto getInBoundsConstIdx = - [&](sol::ArrayType arrTy) -> std::optional { - APInt constIdx; - if (arrTy.isDynSized() || - !matchPattern(getCleanedIdx(), m_ConstantInt(&constIdx)) || - constIdx.uge(arrTy.getSize())) - return std::nullopt; - return constIdx; - }; - sol::DataLocation dataLoc = sol::getDataLocation(baseAddrTy); - Value res; - - switch (dataLoc) { - case sol::DataLocation::Stack: { - auto stkPtrTy = - LLVM::LLVMPointerType::get(r.getContext(), evm::AddrSpace_Stack); - res = r.create(loc, /*resultType=*/stkPtrTy, - /*basePtrType=*/remappedBaseAddr.getType(), - remappedBaseAddr, idx); - break; - } - - case sol::DataLocation::Storage: { - // Storage array - if (auto arrTy = dyn_cast(baseAddrTy)) { - [[maybe_unused]] Type eltTy = arrTy.getEltType(); - assert((isa(eltTy) || isa(eltTy) || - sol::isAddressLikeType(eltTy) || isa(eltTy) || - isa(eltTy) || - isa(eltTy) || - sol::isNonPtrRefType(eltTy)) && - "NYI"); - - // Bounds check (always for dynamic, unless an in-bounds constant index - // for static). - bool needsBoundsCheck = !getInBoundsConstIdx(arrTy); - Value arrayIdx = getCleanedIdx(); - - // The element address of a storage array: the bounds check, the - // keccak256 of the data slot for a dynamic array, and the stride or - // packed-slot arithmetic. \p slot and \p index are the array's slot - // and the already-cleaned index. Everything is built at \p l: the - // op's location inline, the helper's location in a helper. - auto genElementAddr = [&](Value slot, Value index, - Location l) -> Value { - if (needsBoundsCheck) { - Value size = arrTy.isDynSized() - ? evmB.genDynSize(slot, baseAddrTy, l) - : bExt.genI256Const(arrTy.getSize(), l); - auto panicCond = - bExt.genCmp(yul::CmpPredicate::uge, index, size, l); - genBoundsCheck(panicCond, l); - } - - // Base slot: keccak256(slot) for dynamic, slot for static. - Value baseSlot = arrTy.isDynSized() - ? evmB.genDataAddrPtr(slot, baseAddrTy, l) - : slot; - return evmB.genStorageEltAddr(baseSlot, index, arrTy, l); - }; - - // An access that carries a bounds check has a body worth sharing: the - // check, a keccak256 for dynamic arrays, and the addressing. Emit it - // once per (array type, bounds behaviour) and call. An in-bounds - // constant index into a static array needs no check and stays inline. - if (needsBoundsCheck) { - // The result is the legalized element pointer: a {slot, byte - // offset} struct for a packed element, one word otherwise. - Type resTy = getTypeConverter()->convertType(op.getType()); - res = evmB.genHelperCall( - evm::helpersym::indexStorageArray(arrTy, op.getNoPanicBounds()), - {remappedBaseAddr, arrayIdx}, resTy, - [&](ValueRange args, Location bodyLoc) { - return genElementAddr(args[0], args[1], bodyLoc); - }); - } else { - res = genElementAddr(remappedBaseAddr, arrayIdx, loc); - } - - } else if (auto structTy = dyn_cast(baseAddrTy)) { - // Field index is always constant for struct access. - auto constIdx = cast(idx.getDefiningOp()); - uint64_t fieldIdx = constIdx.getValue().getZExtValue(); - - ArrayRef memberTypes = structTy.getMemberTypes(); - assert(fieldIdx < memberTypes.size() && - "struct field index out of range"); - Type fieldTy = memberTypes[fieldIdx]; - auto [slotOffset, byteOffset] = - structTy.getStorageMemberOffset(fieldIdx); - - // Position for target field. - if (sol::canBePacked(fieldTy)) { - // Result: {baseSlot + slotOffset, byteOffset} - Value slot = r.create(loc, remappedBaseAddr, - bExt.genI256Const(slotOffset)); - res = bExt.genLLVMStruct({slot, bExt.genI256Const(byteOffset)}); - } else { - res = r.create(loc, remappedBaseAddr, - bExt.genI256Const(slotOffset)); - } - - } else if (isa(baseAddrTy)) { - res = evmB.genStringItemAddress(remappedBaseAddr, getCleanedIdx(), loc); - } else { - llvm_unreachable("NYI"); - } - - assert(res); - break; - } - - case sol::DataLocation::CallData: - case sol::DataLocation::Memory: { - // Memory/calldata array - if (auto arrTy = dyn_cast(baseAddrTy)) { - Value addrAtIdx; - - // Don't generate out-of-bounds check for in-bounds constant indexing of - // static arrays. - if (std::optional constIdx = getInBoundsConstIdx(arrTy)) { - APInt stride = evm::getArrayEltStride(arrTy); - addrAtIdx = r.create( - loc, remappedBaseAddr, bExt.genI256Const(*constIdx * stride)); - } else { - // - // Generate PanicCode::ArrayOutOfBounds check. - // - Value size; - if (arrTy.isDynSized()) - size = evmB.genDynSize(remappedBaseAddr, baseAddrTy); - else - size = bExt.genI256Const(arrTy.getSize()); - - // Generate `if iszero(lt(index, (baseRef)))` (yul). - auto panicCond = - bExt.genCmp(yul::CmpPredicate::uge, getCleanedIdx(), size); - genBoundsCheck(panicCond); - - // - // Generate the address. - // - // In memory every element occupies one 32-byte slot: value types are - // stored directly, reference types (arrays, structs) are stored as - // pointers. In calldata the stride is the ABI head size of the - // element type. - Value stride = bExt.genI256Const(evm::getArrayEltStride(arrTy)); - Value scaledIdx = r.create(loc, getCleanedIdx(), stride); - if (arrTy.isDynSized()) { - Value dataAddr = evmB.genDataAddrPtr(remappedBaseAddr, baseAddrTy); - addrAtIdx = r.create(loc, dataAddr, scaledIdx); - } else { - addrAtIdx = r.create(loc, remappedBaseAddr, scaledIdx); - } - } - - // For calldata pointers to inner elements that have dynamic content, - // resolve the ABI relative-offset word in the head area. - Type eltTy = arrTy.getEltType(); - if (dataLoc == sol::DataLocation::CallData && - sol::hasDynamicallySizedElt(eltTy)) { - Value outerDataBase = - arrTy.isDynSized() - ? evmB.genDataAddrPtr(remappedBaseAddr, baseAddrTy) - : remappedBaseAddr; - addrAtIdx = evmB.genCalldataAccessRef(eltTy, outerDataBase, addrAtIdx, - /*isNonABI=*/true, loc); - } - - res = addrAtIdx; - - // Memory/calldata struct - } else if (auto structTy = dyn_cast(baseAddrTy)) { - auto idxConstOp = cast(idx.getDefiningOp()); - uint64_t fieldIdx = idxConstOp.getValue().getZExtValue(); - - // In memory every member occupies exactly 32 bytes in the head area. - // In calldata, members occupy their ABI head size: fixed-length arrays - // are inline only when their elements are statically sized, while - // dynamically encoded members contribute a single 32-byte offset. So - // we must accumulate getCallDataHeadSize() over preceding members - // rather than multiplying fieldIdx by 32. - unsigned byteOffset = 0; - ArrayRef memberTypes = structTy.getMemberTypes(); - assert(fieldIdx < memberTypes.size() && - "struct field index out of range"); - if (dataLoc == sol::DataLocation::CallData) { - for (uint64_t i = 0; i < fieldIdx; ++i) - byteOffset += evm::getCallDataHeadSize(memberTypes[i]); - } else { - byteOffset = fieldIdx * 32; - } - res = r.create(loc, remappedBaseAddr, - bExt.genI256Const(byteOffset)); - - // For calldata structs, resolve ABI relative-offset for dynamic - // members. - if (dataLoc == sol::DataLocation::CallData) { - Type memberTy = memberTypes[fieldIdx]; - if (sol::hasDynamicallySizedElt(memberTy)) - res = evmB.genCalldataAccessRef(memberTy, remappedBaseAddr, res, - /*isNonABI=*/true, loc); - } - // Bytes (!sol.string) - } else if (auto strTy = dyn_cast(baseAddrTy)) { - Value size = evmB.genDynSize(remappedBaseAddr, baseAddrTy); - auto panicCond = - bExt.genCmp(yul::CmpPredicate::uge, getCleanedIdx(), size); - genBoundsCheck(panicCond); - - Value dataAddr = evmB.genDataAddrPtr(remappedBaseAddr, baseAddrTy); - res = r.create(loc, dataAddr, getCleanedIdx()); - } - - assert(res); - break; - } - - default: - llvm_unreachable("NYI"); - break; - } - - r.replaceOp(op, res); - return success(); - } -}; - -struct MapOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::MapOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - evm::Builder evmB(getModule(op), r, loc); - - // Assert that the mapping is a slot (result of sol.addr_of or sol.map). - assert(cast(adaptor.getMapping().getType()).getWidth() == 256); - - Type keyTy = op.getKey().getType(); - Value slot; - - if (isa(keyTy)) { - // Dynamic key types (string, bytes): hash = keccak256(abi.encodePacked( - // rawBytes, slot)) - Value pos = evmB.genFreePtr(loc); - Value dataLen = - evmB.genCopyStringDataToMemory(adaptor.getKey(), keyTy, pos, loc); - Value slotAddr = r.create(loc, pos, dataLen); - r.create(loc, slotAddr, adaptor.getMapping()); - Value totalLen = - r.create(loc, dataLen, bExt.genI256Const(0x20)); - slot = r.create(loc, pos, totalLen); - } else { - // Static key types (integers, address, contract, enum, fixedbytes, byte, - // ext func ref): value-type key left/right-aligned in 32 bytes, followed - // by slot. hash = keccak256(key_word, slot_word). - auto zero = bExt.genI256Const(0); - Value key = evmB.genCleanup(keyTy, adaptor.getKey(), loc); - r.create(loc, zero, key); - r.create(loc, bExt.genI256Const(0x20), - adaptor.getMapping()); - slot = r.create(loc, zero, bExt.genI256Const(0x40)); - } - - // Result is {slot, 0} or just slot depending on pointee type. - Type resTy = op.getResult().getType(); - if (auto ptrTy = dyn_cast(resTy); - ptrTy && ptrTy.getDataLocation() == sol::DataLocation::Storage && - sol::canBePacked(ptrTy.getPointeeType())) { - r.replaceOp(op, bExt.genLLVMStruct({slot, bExt.genI256Const(0)})); - } else { - r.replaceOp(op, slot); - } - return success(); - } -}; - -struct LoadOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::LoadOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - evm::Builder evmB(getModule(op), r, loc); - - Value addr = adaptor.getAddr(); - sol::DataLocation dataLoc = sol::getDataLocation(op.getAddr().getType()); - auto i256Ty = r.getIntegerType(256); - Type eltTy = - cast(op.getAddr().getType()).getPointeeType(); - - switch (dataLoc) { - case sol::DataLocation::Stack: { - Type loadTy = getTypeConverter()->convertType(eltTy); - assert(loadTy); - r.replaceOpWithNewOp(op, loadTy, addr, - evm::getAlignment(addr)); - return success(); - } - case sol::DataLocation::Immutable: { - r.replaceOp(op, evmB.genLoad(addr, dataLoc)); - return success(); - } - case sol::DataLocation::CallData: { - Value ld = evmB.genLoad(addr, dataLoc); - if (isa(eltTy)) { - evmB.genExtFuncWordValidation(ld, loc); - ld = evmB.genExtFuncUnpack(ld, /*inStorage=*/false, loc); - } else if (isa(eltTy) || isa(eltTy) || - sol::isAddressLikeType(eltTy) || sol::isBytesLikeType(eltTy)) { - ld = evmB.genCleanup(eltTy, ld, loc, dataLoc); - } - - r.replaceOp(op, ld); - return success(); - } - case sol::DataLocation::Memory: { - Value ld = evmB.genLoad(addr, dataLoc); - // old codegen begin - // Only do cleanup for byte type. This matches the old codegen. - // (Via-IR cleans every memory load). - if (isa(eltTy)) - ld = evmB.genCleanup(eltTy, ld, loc, dataLoc); - // old codegen end - if (isa(eltTy)) - ld = evmB.genExtFuncUnpack(ld, /*inStorage=*/false, loc); - - r.replaceOp(op, ld); - return success(); - } - case sol::DataLocation::Storage: - case sol::DataLocation::Transient: { - // Helper to emit sload or tload based on data location. - auto genSlotLoad = [&](Value slot) -> Value { - if (dataLoc == sol::DataLocation::Transient) - return r.create(loc, slot); - return r.create(loc, slot); - }; - - if (sol::canBePacked(eltTy)) { - // addr is {slot, offset} struct - Value slot = r.create( - loc, i256Ty, addr, r.getDenseI64ArrayAttr({0})); - Value offset = r.create( - loc, i256Ty, addr, r.getDenseI64ArrayAttr({1})); - - // Load the full slot and shift right by (offset * 8) to extract. - Value slotVal = genSlotLoad(slot); - Value shiftBits = - r.create(loc, offset, bExt.genI256Const(8)); - Value shifted = r.create(loc, slotVal, shiftBits); - Value cleaned = evmB.genCleanupPackedStorageValue(eltTy, shifted, loc); - r.replaceOp(op, cleaned); - } else { - // addr is just slot - Value slotVal = genSlotLoad(addr); - r.replaceOp(op, slotVal); - } - return success(); - } - }; - } -}; - -struct LoadImmutableOpLowering : public OpRewritePattern { - using OpRewritePattern::OpRewritePattern; - - LogicalResult matchAndRewrite(sol::LoadImmutableOp op, - PatternRewriter &r) const override { - r.replaceOpWithNewOp(op, op.getName()); - return success(); - } -}; - -struct StoreOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::StoreOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - evm::Builder evmB(getModule(op), r, loc); - - Value remappedVal = adaptor.getVal(); - Value remappedAddr = adaptor.getAddr(); - sol::DataLocation dataLoc = sol::getDataLocation(op.getAddr().getType()); - Type eltTy = - cast(op.getAddr().getType()).getPointeeType(); - - switch (dataLoc) { - case sol::DataLocation::Stack: - r.replaceOpWithNewOp(op, remappedVal, remappedAddr, - evm::getAlignment(remappedAddr)); - return success(); - case sol::DataLocation::Immutable: - case sol::DataLocation::Memory: { - // Generate mstore8 for storing to `bytes`. - if (isa(eltTy) && dataLoc == sol::DataLocation::Memory) { - auto byteVal = - r.create(loc, bExt.genI256Const(0), remappedVal); - r.replaceOpWithNewOp(op, remappedAddr, byteVal); - return success(); - } - - if (isa(eltTy)) { - remappedVal = - evmB.genExtFuncPack(remappedVal, /*inStorage=*/false, loc); - } else if (isa(eltTy) || isa(eltTy) || - sol::isAddressLikeType(eltTy) || - isa(eltTy)) { - remappedVal = evmB.genCleanup(eltTy, remappedVal, loc); - } - - evmB.genStore(remappedVal, remappedAddr, dataLoc); - r.eraseOp(op); - return success(); - } - case sol::DataLocation::Storage: - case sol::DataLocation::Transient: { - auto i256Ty = r.getIntegerType(256); - - // Helper to create sstore or tstore based on data location. - auto genSlotStore = [&](Value slot, Value val) { - if (dataLoc == sol::DataLocation::Transient) - r.create(loc, slot, val); - else - r.create(loc, slot, val); - }; - - if (sol::canBePacked(eltTy)) { - // addr is {slot, offset} struct - Value slot = r.create( - loc, i256Ty, remappedAddr, r.getDenseI64ArrayAttr({0})); - Value offset = r.create( - loc, i256Ty, remappedAddr, r.getDenseI64ArrayAttr({1})); - - // Cleanup the value to be stored. Skip for ext-fn-ref as upstream does - // the same (the cleanup happens at packing) - Value preparedVal = isa(eltTy) - ? remappedVal - : evmB.genCleanup(eltTy, remappedVal, loc); - unsigned numBits; - if (sol::isBytesLikeType(eltTy)) { - // Bytes-like types: shr to convert from MSB-aligned to LSB-aligned. - numBits = sol::getNumBytes(eltTy) * 8; - preparedVal = r.create( - loc, preparedVal, bExt.genI256Const(256 - numBits)); - } else if (sol::isAddressLikeType(eltTy)) { - numBits = 160; - } else if (auto intTy = dyn_cast(eltTy)) { - // In storage packing, bool occupies 1 byte not a single bit. - numBits = intTy.getWidth() == 1 ? 8 : intTy.getWidth(); - - // Signed integer cleanup sign-extends to i256. Packed storage stores - // only the field slice, so drop the sign-extended bits before shl/or. - if (intTy.isSigned() && numBits < 256) - preparedVal = r.create( - loc, preparedVal, - bExt.genI256Const(APInt::getLowBitsSet(256, numBits))); - } else if (isa(eltTy)) { - // FuncRef is 64 bits, already i256. - numBits = 64; - } else if (isa(eltTy)) { - // ExtFuncRef is bytes24 here. - numBits = 192; - preparedVal = - evmB.genExtFuncPack(preparedVal, /*inStorage=*/true, loc); - } else if (isa(eltTy)) { - // Enums can have at most 256 members, so always 1 byte. - numBits = 8; - } else { - llvm_unreachable("NYI"); - } - - // Punch hole in slot for new value. - Value shiftBits = - r.create(loc, offset, bExt.genI256Const(8)); - Value slotWithHole = - evmB.genPunchHole(slot, shiftBits, numBits, dataLoc); - - // Shift new value to position: shl(offset * 8, preparedVal) - preparedVal = r.create(loc, preparedVal, shiftBits); - - // Combine, i.e. or(slotWithHole, preparedVal) and store. - genSlotStore(slot, r.create(loc, slotWithHole, preparedVal)); - } else { - // addr is just slot, do direct store. - genSlotStore(remappedAddr, remappedVal); - } - r.eraseOp(op); - return success(); - } - default: - break; - }; - - llvm_unreachable("NYI: Calldata data-location"); - } -}; - -struct DataLocCastOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::DataLocCastOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Type srcTy = op.getInp().getType(); - assert(!isa(srcTy)); - assert(sol::getDataLocation(op.getType()) == sol::DataLocation::Memory && - sol::isNonPtrRefType(srcTy) && "unexpected data location cast"); - - Location loc = op.getLoc(); - evm::Builder evmB(getModule(op), r, loc); - sol::DataLocation srcDataLoc = sol::getDataLocation(srcTy); - - // A calldata struct, or a calldata array of dynamically encoded elements, - // is copied to memory by decoding it against the end of calldata, which - // validates every nested offset and length. - // old codegen begin - // Every other calldata array and bytes stays a raw copy with no checks. - // (Via-IR decodes every calldata array copy.) - auto srcArrTy = dyn_cast(srcTy); - bool decodesFromCallData = - isa(srcTy) || - (srcArrTy && sol::hasDynamicallySizedElt(srcArrTy.getEltType())); - // old codegen end - if (srcDataLoc == sol::DataLocation::CallData && decodesFromCallData) { - mlir::solgen::BuilderExt bExt(r, loc); - Value src = adaptor.getInp(); - // The decoder reads a dynamic array from its length word, which precedes - // the data of every calldata array: a slice cannot have a dynamically - // encoded element type. - Value addr = src; - if (srcArrTy && srcArrTy.isDynSized()) - addr = r.create(loc, evmB.genDataAddrPtr(src, srcTy), - bExt.genI256Const(32)); - r.replaceOp(op, evmB.genABITupleDecoding( - op.getType(), addr, /*fromMem=*/false, - /*tupleEnd=*/r.create(loc), - /*topLevel=*/false, loc)); - return success(); - } - - r.replaceOp(op, evmB.genCloneToMemory(srcTy, adaptor.getInp(), srcDataLoc)); - return success(); - } -}; - -struct LengthOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::LengthOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - evm::Builder evmB(getModule(op), r, loc); - - Type ty = op.getInp().getType(); - - if (auto stringTy = dyn_cast(ty)) { - r.replaceOp(op, evmB.genDynSize(adaptor.getInp(), ty)); - return success(); - } - if (auto arrTy = dyn_cast(ty)) { - if (arrTy.isDynSized()) { - r.replaceOp(op, evmB.genDynSize(adaptor.getInp(), ty)); - return success(); - } - r.replaceOp(op, bExt.genI256Const(arrTy.getSize())); - return success(); - } - llvm_unreachable("NYI"); - } -}; - -struct SliceOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::SliceOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - evm::Builder evmB(getModule(op), r, loc); - - Type arrTy = op.getArr().getType(); - assert(sol::getDataLocation(arrTy) == sol::DataLocation::CallData); - - Value arr = adaptor.getArr(); - Value start = - evmB.genCleanup(op.getStart().getType(), adaptor.getStart(), loc); - Value end = evmB.genCleanup(op.getEnd().getType(), adaptor.getEnd(), loc); - - Value dataAddr = evmB.genDataAddrPtr(arr, arrTy); - Value length = evmB.genDynSize(arr, arrTy); - - // Validate: start <= end - auto startGtEnd = bExt.genCmp(yul::CmpPredicate::ugt, start, end); - evmB.genDebugRevertWithMsg(startGtEnd, "Slice starts after end", loc); - - // Validate: end <= length - auto endGtLen = bExt.genCmp(yul::CmpPredicate::ugt, end, length); - evmB.genDebugRevertWithMsg(endGtLen, "Slice is greater than length", loc); - - // Compute stride (element size in calldata) - unsigned stride = 32; - if (auto arrType = dyn_cast(arrTy)) - stride = evm::getCallDataHeadSize(arrType.getEltType()); - else if (isa(arrTy)) - stride = 1; - - // newOffset = dataAddr + start * stride - Value scaledStart = - r.create(loc, start, bExt.genI256Const(stride)); - Value newOffset = r.create(loc, dataAddr, scaledStart); - - // newLength = end - start - Value newLength = r.create(loc, end, start); - - r.replaceOp(op, bExt.genLLVMStruct({newOffset, newLength})); - return success(); - } -}; - -struct CopyOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::CopyOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - evm::Builder evmB(getModule(op), r, loc); - - if (isa(op.getSrc().getType())) { - StringRef bytes = - cast(op.getSrc().getDefiningOp()).getValue(); - evmB.genCopyConstantStringToStorage(bytes, adaptor.getDst(), loc); - r.eraseOp(op); - return success(); - } - - Type srcTy = op.getSrc().getType(); - Type dstTy = op.getDst().getType(); - sol::DataLocation srcDataLoc = sol::getDataLocation(srcTy); - sol::DataLocation dstDataLoc = sol::getDataLocation(dstTy); - - evmB.genCopy(srcTy, dstTy, adaptor.getSrc(), adaptor.getDst(), srcDataLoc, - dstDataLoc, loc); - r.eraseOp(op); - return success(); - } -}; - -struct DeleteOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::DeleteOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - evm::Builder evmB(getModule(op), r, loc); - - // The op's operand type is the storage value to clear; its remapped operand - // (AddrOfOp lowered to an i256) is the slot to clear at. - Type refTy = op.getReference().getType(); - assert(sol::getDataLocation(refTy) == sol::DataLocation::Storage && - "sol.delete expects a Storage reference"); - evmB.genClearStorageValue(refTy, adaptor.getReference(), loc); - r.eraseOp(op); - return success(); - } -}; - -struct ThisOpLowering : public OpRewritePattern { - using OpRewritePattern::OpRewritePattern; - - LogicalResult matchAndRewrite(sol::ThisOp op, - PatternRewriter &r) const override { - r.replaceOpWithNewOp(op); - return success(); - } -}; - -struct CallerOpLowering : public OpRewritePattern { - using OpRewritePattern::OpRewritePattern; - - LogicalResult matchAndRewrite(sol::CallerOp op, - PatternRewriter &r) const override { - r.replaceOpWithNewOp(op); - return success(); - } -}; - -struct OriginOpLowering : public OpRewritePattern { - using OpRewritePattern::OpRewritePattern; - - LogicalResult matchAndRewrite(sol::OriginOp op, - PatternRewriter &r) const override { - r.replaceOpWithNewOp(op); - return success(); - } -}; - -struct GasPriceOpLowering : public OpRewritePattern { - using OpRewritePattern::OpRewritePattern; - - LogicalResult matchAndRewrite(sol::GasPriceOp op, - PatternRewriter &r) const override { - r.replaceOpWithNewOp(op); - return success(); - } -}; - -struct CallValueOpLowering : public OpRewritePattern { - using OpRewritePattern::OpRewritePattern; - - LogicalResult matchAndRewrite(sol::CallValueOp op, - PatternRewriter &r) const override { - r.replaceOpWithNewOp(op); - return success(); - } -}; - -struct SigOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::SigOp op, OpAdaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - Value zero = bExt.genI256Const(0); - Value data = r.create(loc, zero); - // Mask to keep only the top 4 bytes (bytes4 is MSB-aligned). - Value mask = bExt.genI256Const(llvm::APInt::getHighBitsSet(256, 32)); - r.replaceOp(op, r.create(loc, data, mask)); - return success(); - } -}; - -struct BaseFeeOpLowering : public OpRewritePattern { - using OpRewritePattern::OpRewritePattern; - LogicalResult matchAndRewrite(sol::BaseFeeOp op, - PatternRewriter &r) const override { - r.replaceOpWithNewOp(op); - return success(); - } -}; - -struct BlobBaseFeeOpLowering : public OpRewritePattern { - using OpRewritePattern::OpRewritePattern; - LogicalResult matchAndRewrite(sol::BlobBaseFeeOp op, - PatternRewriter &r) const override { - r.replaceOpWithNewOp(op); - return success(); - } -}; - -struct ChainIdOpLowering : public OpRewritePattern { - using OpRewritePattern::OpRewritePattern; - LogicalResult matchAndRewrite(sol::ChainIdOp op, - PatternRewriter &r) const override { - r.replaceOpWithNewOp(op); - return success(); - } -}; - -struct CoinbaseOpLowering : public OpRewritePattern { - using OpRewritePattern::OpRewritePattern; - LogicalResult matchAndRewrite(sol::CoinbaseOp op, - PatternRewriter &r) const override { - r.replaceOpWithNewOp(op); - return success(); - } -}; - -struct DifficultyOpLowering : public OpRewritePattern { - using OpRewritePattern::OpRewritePattern; - LogicalResult matchAndRewrite(sol::DifficultyOp op, - PatternRewriter &r) const override { - r.replaceOpWithNewOp(op); - return success(); - } -}; - -struct GasLimitOpLowering : public OpRewritePattern { - using OpRewritePattern::OpRewritePattern; - LogicalResult matchAndRewrite(sol::GasLimitOp op, - PatternRewriter &r) const override { - r.replaceOpWithNewOp(op); - return success(); - } -}; - -struct BlockNumberOpLowering : public OpRewritePattern { - using OpRewritePattern::OpRewritePattern; - LogicalResult matchAndRewrite(sol::BlockNumberOp op, - PatternRewriter &r) const override { - r.replaceOpWithNewOp(op); - return success(); - } -}; - -struct PrevRandaoOpLowering : public OpRewritePattern { - using OpRewritePattern::OpRewritePattern; - LogicalResult matchAndRewrite(sol::PrevRandaoOp op, - PatternRewriter &r) const override { - r.replaceOpWithNewOp(op); - return success(); - } -}; - -struct TimestampOpLowering : public OpRewritePattern { - using OpRewritePattern::OpRewritePattern; - LogicalResult matchAndRewrite(sol::TimestampOp op, - PatternRewriter &r) const override { - r.replaceOpWithNewOp(op); - return success(); - } -}; - -struct GasLeftOpLowering : public OpRewritePattern { - using OpRewritePattern::OpRewritePattern; - LogicalResult matchAndRewrite(sol::GasLeftOp op, - PatternRewriter &r) const override { - r.replaceOpWithNewOp(op); - return success(); - } -}; - -struct BlockHashOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - LogicalResult matchAndRewrite(sol::BlockHashOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - r.replaceOpWithNewOp(op, adaptor.getBlockNumber()); - return success(); - } -}; - -struct BlobHashOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - LogicalResult matchAndRewrite(sol::BlobHashOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - r.replaceOpWithNewOp(op, adaptor.getIdx()); - return success(); - } -}; - -struct SelfdestructOpLowering - : public CleanedOperandsLowering { - using Base = - CleanedOperandsLowering; - using Base::Base; - - LogicalResult rewriteCleaned(sol::SelfdestructOp op, - ConversionPatternRewriter &r, - Value recipient) const { - r.replaceOpWithNewOp(op, recipient); - return success(); - } -}; - -struct LibAddrOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::LibAddrOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - r.replaceOpWithNewOp(op, op.getName()); - return success(); - } -}; - -// old codegen begin -// (Via-IR cleans on casts, not here). -struct CodeHashOpLowering - : public CleanedOperandsLowering { - using Base = CleanedOperandsLowering; - using Base::Base; - - LogicalResult rewriteCleaned(sol::CodeHashOp op, ConversionPatternRewriter &r, - Value addr) const { - r.replaceOpWithNewOp(op, addr); - return success(); - } -}; - -struct BalanceOpLowering - : public CleanedOperandsLowering { - using Base = CleanedOperandsLowering; - using Base::Base; - - LogicalResult rewriteCleaned(sol::BalanceOp op, ConversionPatternRewriter &r, - Value addr) const { - r.replaceOpWithNewOp(op, addr); - return success(); - } -}; -// old codegen end - -struct EncodeOpLowering : public OpConversionPattern { - EncodeOpLowering(TypeConverter &tyConv, MLIRContext *ctx, - evm::StringLiteralPool &literals) - : OpConversionPattern(tyConv, ctx), literals(literals) {} - - LogicalResult matchAndRewrite(sol::EncodeOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - evm::Builder evmB(getModule(op), r, loc, literals); - - // An encoding that only the next op hashes is read before anything else - // allocates, so it takes no memory of its own. - Operation *user = op->hasOneUse() ? *op->user_begin() : nullptr; - bool reserve = - !isa_and_nonnull(user) || user != op->getNextNode(); - - Value freePtr = evmB.genFreePtr(); - if (op.getPacked()) { - Value dataStart = - r.create(loc, freePtr, bExt.genI256Const(32)); - Value dataEnd = evmB.genABIEncodingPacked(op.getIns().getType(), - adaptor.getIns(), dataStart); - Value dataSize = r.create(loc, dataEnd, dataStart); - r.create(loc, freePtr, dataSize); - if (reserve) - evmB.genFreePtrStore(dataEnd); - } else if (op.getSelector()) { - assert(adaptor.getSelector() && "selector operand is required"); - - Value selectorAddr = - r.create(loc, freePtr, bExt.genI256Const(32)); - r.create(loc, selectorAddr, adaptor.getSelector()); - - Value tupleStart = - r.create(loc, selectorAddr, bExt.genI256Const(4)); - Value tupleEnd = evmB.genABIEncoding(op.getIns().getType(), - adaptor.getIns(), tupleStart); - Value dataSize = r.create(loc, tupleEnd, selectorAddr); - r.create(loc, freePtr, dataSize); - if (reserve) - evmB.genFreePtrStore(tupleEnd); - } else { - Value tupleStart = - r.create(loc, freePtr, bExt.genI256Const(32)); - Value tupleEnd = evmB.genABIEncoding(op.getIns().getType(), - adaptor.getIns(), tupleStart); - Value tupleSize = r.create(loc, tupleEnd, tupleStart); - r.create(loc, freePtr, tupleSize); - if (reserve) - evmB.genFreePtrStore(tupleEnd); - } - r.replaceOp(op, freePtr); - return success(); - } - - evm::StringLiteralPool &literals; -}; - -struct DecodeOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::DecodeOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - evm::Builder evmB(getModule(op), r, loc); - - std::vector results; - Type srcTy = op.getAddr().getType(); - auto srcStringTy = dyn_cast(srcTy); - assert(srcStringTy && - "abi.decode source must be bytes memory/calldata in sol dialect"); - sol::DataLocation dataLoc = srcStringTy.getDataLocation(); - assert((dataLoc == sol::DataLocation::Memory || - dataLoc == sol::DataLocation::CallData) && - "abi.decode expects memory/calldata byte buffer"); - - Value tupleStart = evmB.genDataAddrPtr(adaptor.getAddr(), srcTy); - Value tupleSize = evmB.genDynSize(adaptor.getAddr(), srcTy); - Value tupleEnd = r.create(loc, tupleStart, tupleSize); - bool fromMem = dataLoc == sol::DataLocation::Memory; - evmB.genABITupleDecoding(op.getResultTypes(), tupleStart, tupleEnd, results, - fromMem, *getTypeConverter()); - r.replaceOp(op, results); - return success(); - } -}; - -struct ExtCallOpLowering : public OpConversionPattern { - ExtCallOpLowering(TypeConverter &tyConv, MLIRContext *ctx, - evm::StringLiteralPool &literals) - : OpConversionPattern(tyConv, ctx), literals(literals) {} - - LogicalResult matchAndRewrite(sol::ExtCallOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - auto mod = getModule(op); - evm::Builder evmB(mod, r, loc, literals); - - assert(sol::evmCanOverchargeGasForCall(mod) && "NYI"); - - // TODO: - // - The return arg analysis is done for evm's the supports return data (See - // solidity::frontend::ReturnInfo). - // - The generated code for the return has returndatacopy and returnsize. - assert(sol::evmSupportsReturnData(mod) && "NYI"); - - SmallVector abiInputTys = - getABITypes(r, op.getIns().getTypes(), op.getCalleeType().getInputs(), - op.getLibraryCall()); - // Decode into the types the caller holds (op results; the leading i1 is - // the status flag), not the declared callee results - calldata-typed - // returns land in memory on the caller side. - SmallVector callerResultTys(llvm::drop_begin(op.getResultTypes())); - SmallVector abiResultTys = - getABITypes(r, callerResultTys, callerResultTys, op.getLibraryCall()); - - // Check if we need to generate the extcodesize check. - unsigned totHeadSize = 0; - for (auto resTy : abiResultTys) { - totHeadSize += evm::getCallDataHeadSize(resTy); - } - - bool extCodeSizeCheck = totHeadSize == 0 || - !sol::evmSupportsReturnData(mod) || - sol::shouldEmitDebugRevertStrings(mod); - - Value addr = adaptor.getAddr(); - // old codegen begin - // (Via-IR cleans on casts, not here). - if (isa(op.getAddr().getType())) - addr = evmB.genCleanup(op.getAddr().getType(), addr, loc); - // old codegen end + // Int to bytes + assert(isa(inpTy)); + unsigned outBytesSize = sol::getNumBytes(outTy); + auto shiftAmt = bExt.genI256Const(256 - (8 * outBytesSize)); + return b.create(loc, inp, shiftAmt); +} - if (extCodeSizeCheck) { - // Generate the revert code. - auto extCodeSize = r.create(loc, addr); - auto isExtCodeSizeZero = - bExt.genCmp(yul::CmpPredicate::eq, extCodeSize, bExt.genI256Const(0)); - evmB.genDebugRevertWithMsg(isExtCodeSizeZero, - "Target contract does not contain code"); - } +Value Lowering::lower(sol::DynBytesToFixedBytesOp op) { + return evmBuilder(op.getLoc()) + .genDynBytesToFixedBytes(lowered(op.getInp()), op.getInp().getType(), + cast(op.getType()), + op.getLoc()); +} - // Generate the store of the selector. - Value selectorAddr = evmB.genFreePtr(); - Value shiftedSelector = r.create( - loc, adaptor.getSelector(), bExt.genI256Const(224)); - r.create(loc, selectorAddr, shiftedSelector); +Value Lowering::lower(sol::FixedBytesIndexOp op) { + Location loc = op.getLoc(); + solgen::BuilderExt bExt(b, loc); + unsigned numBytes = + cast(op.getValue().getType()).getSize(); + // Only the index is cleaned. The bounds check keeps `byte` within the + // leading numBytes bytes, and dirty bits only sit in the trailing ones. + Value idx = evmBuilder(loc).genCleanup(op.getIndex().getType(), + lowered(op.getIndex()), loc); + Value val = lowered(op.getValue()); + + // Bounds check, mirroring the legacy codegen: index >= numBytes panics + // with ArrayOutOfBounds. + Value oob = bExt.genCmp(yul::CmpPredicate::uge, idx, + bExt.genI256Const(numBytes, loc), loc); + evmBuilder(loc).genPanic(mlir::evm::PanicCode::ArrayOutOfBounds, oob, loc); + + // `byte(idx, val)` extracts the idx-th byte (from the MSB) right-aligned; + // shift it left by 248 bits to left-align it as a bytes1 value. + Value byteVal = b.create(loc, idx, val); + return b.create(loc, bExt.genI256Const(248, loc), byteVal); +} - // Generate the abi encoding code. - Value tupleStart = - r.create(loc, selectorAddr, bExt.genI256Const(4)); - Value tupleEnd = - evmB.genABIEncoding(abiInputTys, adaptor.getIns(), tupleStart); - - // Calculate the return size statically and/or check if it's dynamic. This - // is copied from solidity::frontend::ReturnInfo. - unsigned staticRetSizeVal = 0; - bool isRetSizeDynamic = false; - for (Type ty : abiResultTys) { - // A struct/array with a dynamically-sized element encodes as a tail - // whose size isn't static, even though the type itself isn't - // "dynamically sized" in ABI terms. - if (sol::hasDynamicallySizedElt(ty)) { - isRetSizeDynamic = true; - staticRetSizeVal = 0; - break; - } - staticRetSizeVal += evm::getCallDataHeadSize(ty); - } +Value Lowering::lower(sol::ExtFuncConstantOp op) { + Location loc = op.getLoc(); + Value undef = b.create(loc, evm::getYulType(op.getType())); + Value selector = solgen::BuilderExt(b, loc).genI256Const(op.getSelector()); + Value withAddr = b.create( + loc, undef, lowered(op.getAddr()), b.getDenseI64ArrayAttr({0})); + return b.create(loc, withAddr, selector, + b.getDenseI64ArrayAttr({1})); +} - // Generate the call. - Value inpSize = r.create(loc, tupleEnd, selectorAddr); - Value staticRetSize = bExt.genI256Const(staticRetSizeVal); - // A call that names no gas forwards all the gas left, read here so that it - // is not kept live across the encoding. - Value gas = adaptor.getGas() ? adaptor.getGas() : r.create(loc); - mlir::Value status; - - // Order is important here, staticcall might overlap with delegatecall. - if (op.getDelegateCall()) - status = r.create( - loc, gas, addr, - /*inpOffset=*/selectorAddr, inpSize, - /*outOffset=*/selectorAddr, /*outSize=*/staticRetSize); - else if (op.getStaticCall()) - status = r.create(loc, gas, addr, - /*inpOffset=*/selectorAddr, inpSize, - /*outOffset=*/selectorAddr, - /*outSize=*/staticRetSize); - else - status = r.create(loc, gas, addr, adaptor.getVal(), - /*inpOffset=*/selectorAddr, inpSize, - /*outOffset=*/selectorAddr, - /*outSize=*/staticRetSize); - - // Generate forwarding revert if not try-call. - if (!op.getTryCall()) { - auto statusIsZero = - bExt.genCmp(yul::CmpPredicate::eq, status, bExt.genI256Const(0)); - evmB.genForwardingRevert(statusIsZero); - } +Value Lowering::lower(sol::ExtFuncAddrOp op) { + return genPairField(lowered(op.getFunc()), 0, op.getLoc()); +} - // Get the types of the results from the decoding, which should be the same - // as the corsp legal types. - SmallVector decodedResultTys; - if (failed(getTypeConverter()->convertTypes(callerResultTys, - decodedResultTys))) - return failure(); - - // Generate the if-else op of the status that yields the decoded results. - auto statusIsNotZero = - bExt.genCmp(yul::CmpPredicate::ne, status, bExt.genI256Const(0)); - auto statusIfOp = r.create(loc, /*resultTys=*/decodedResultTys, - /*cond=*/statusIsNotZero); - - // Generate the else block (failure) which yields undefs. The undefs will - // not be used during execution as the either control flow will skip the - // sol.try's success block or hit the previous forwarding revert. - r.setInsertionPointToStart(&statusIfOp.getElseRegion().emplaceBlock()); - SmallVector undefYields; - for (Type ty : decodedResultTys) { - undefYields.push_back(r.create(loc, ty)); - } - r.create(loc, undefYields); - - // Generte the then block (success). - r.setInsertionPointToStart(&statusIfOp.getThenRegion().emplaceBlock()); - - // The allocation `selectorAddr` will be reused for the return data. - - // Generate the decoding of the results. - std::vector decodedResults; - if (isRetSizeDynamic) { - Value retDataSize = r.create(loc); - r.create(loc, selectorAddr, - /*src=*/bExt.genI256Const(0), - retDataSize); - evmB.genFreePtrUpd(selectorAddr, retDataSize); - Value tupleEnd = r.create(loc, selectorAddr, retDataSize); - evmB.genABITupleDecoding(abiResultTys, selectorAddr, tupleEnd, - decodedResults, /*fromMem=*/true, - *getTypeConverter()); - } else { - // See https://github.com/ethereum/solidity/pull/12684: the tuple is the - // return data up to the static size, so return data shorter than that - // fails the tuple's size check, and the tuple is otherwise exactly the - // static size. - if (staticRetSizeVal) { - Value retDataSize = r.create(loc); - Value tooShort = - bExt.genCmp(yul::CmpPredicate::ult, retDataSize, staticRetSize); - evmB.genDebugRevertWithMsg(tooShort, - "ABI decoding: tuple data too short"); - } - // Scalar results are decoded onto the stack right away, so the return - // data needs no memory of its own. - if (!llvm::all_of(abiResultTys, - [](Type ty) { return sol::isScalar(ty); })) - evmB.genFreePtrUpd(selectorAddr, staticRetSize); - Value tupleEnd = r.create(loc, selectorAddr, staticRetSize); - evmB.genABITupleDecoding(abiResultTys, selectorAddr, tupleEnd, - decodedResults, - /*fromMem=*/true, *getTypeConverter()); - } - r.create(loc, decodedResults); +Value Lowering::lower(sol::ExtFuncSelectorOp op) { + Location loc = op.getLoc(); + // The bytes4 result is MSB-aligned: bits [255:224] are the selector. + Value sel = genPairField(lowered(op.getFunc()), 1, loc); + return b.create( + loc, sel, solgen::BuilderExt(b, loc).genI256Const(224)); +} - SmallVector newResults{statusIsNotZero}; - newResults.append(statusIfOp.getResults().begin(), - statusIfOp.getResults().end()); - r.replaceOp(op, newResults); - return success(); - } +//===----------------------------------------------------------------------===// +// Crypto +//===----------------------------------------------------------------------===// - evm::StringLiteralPool &literals; -}; +Value Lowering::lower(sol::Keccak256Op op) { + Location loc = op.getLoc(); + evm::Builder evmB = evmBuilder(loc); + Type ty = op.getAddr().getType(); + Value addr = lowered(op.getAddr()); + Value dataLen = evmB.genLoad(addr, sol::getDataLocation(ty)); + Value dataSlot = evmB.genDataAddrPtr(addr, ty); + return b.create(loc, dataSlot, dataLen); +} -template -static LogicalResult lowerBareCallLikeOp(OpT op, AdaptorT adaptor, - ConversionPatternRewriter &r, - evm::StringLiteralPool &literals, - OpBuilderFuncT &&opBuilderFunc) { +Value Lowering::lower(sol::Erc7201Op op) { Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - auto mod = getModule(op); - evm::Builder evmB(mod, r, loc, literals); + solgen::BuilderExt bExt(b, loc); + evm::Builder evmB = evmBuilder(loc); + Type ty = op.getId().getType(); + Value id = lowered(op.getId()); + Value dataLen = evmB.genLoad(id, sol::getDataLocation(ty)); + Value dataSlot = evmB.genDataAddrPtr(id, ty); + Value inner = b.create(loc, dataSlot, dataLen); + Value decremented = b.create(loc, inner, bExt.genI256Const(1)); + // The outer hash input is a single word, so it fits the scratch space. + Value zero = bExt.genI256Const(0); + b.create(loc, zero, decremented); + Value outer = b.create(loc, zero, bExt.genI256Const(32)); + Value mask = b.create(loc, bExt.genI256Const(0xff)); + return b.create(loc, outer, mask); +} - assert(sol::evmSupportsReturnData(mod) && "NYI"); +template +Value Lowering::lowerHashPrecompile(OpT op, uint32_t precompileAddr) { + Location loc = op.getLoc(); + solgen::BuilderExt bExt(b, loc); + evm::Builder evmB = evmBuilder(loc); - Value inpSize, inpStart; - if (isa(op.getInp().getType())) { + sol::DataLocation dataLoc = sol::DataLocation::Memory; + Value zero = bExt.genI256Const(0); + Value retSize = bExt.genI256Const(32); + Value dataLen, dataSlot; + if (isa(op.getData().getType())) { StringRef bytes = - cast(op.getInp().getDefiningOp()).getValue(); - // The callee reads the input only during the call, so it skips the - // allocation a memory literal gets: it is written at the free memory + cast(op.getData().getDefiningOp()).getValue(); + // The precompile reads the bytes only during the call, so they skip the + // allocation a memory literal gets: they are written at the free memory // pointer, and the pointer does not move. - inpStart = evmB.genFreePtr(loc); - evmB.genStringLiteralData(bytes, inpStart, loc); - inpSize = bExt.genI256Const(bytes.size()); + dataSlot = evmB.genFreePtr(); + evmB.genStringLiteralData(bytes, dataSlot); + dataLen = bExt.genI256Const(bytes.size()); } else { - auto inpTy = cast(op.getInp().getType()); - inpSize = evmB.genDynSize(adaptor.getInp(), inpTy, loc); - inpStart = evmB.genDataAddrPtr(adaptor.getInp(), inpTy, loc); + assert(sol::getDataLocation(op.getData().getType()) == dataLoc); + dataLen = evmB.genLoad(lowered(op.getData()), dataLoc); + dataSlot = evmB.genDataAddrPtr(lowered(op.getData()), dataLoc); } - // old codegen begin - // (Via-IR cleans on casts, not here). - Value addr = evmB.genCleanup(op.getAddr().getType(), adaptor.getAddr(), loc); - // old codegen end - Value zero = bExt.genI256Const(0); - // A call that names no gas forwards all the gas left, read here so that it is - // not kept live across the input size and address computation. - Value gas = adaptor.getGas() ? adaptor.getGas() : r.create(loc); - Value rawStatus = - opBuilderFunc(loc, adaptor, gas, addr, inpStart, inpSize, zero, r); - - Value status = bExt.genCmp(yul::CmpPredicate::ne, rawStatus, zero); - // Return data nobody reads is not copied, whatever its size. - Value retData = op.getRetData().use_empty() - ? bExt.genI256Const(mlir::evm::MemoryLayout::zeroPointer) - : evmB.genReturnDataBytes(loc); - r.replaceOp(op, SmallVector{status, retData}); - return success(); -} + Value gas = b.create(loc); + // Hashing functions store their result in scratch space (0x00-0x3f). + Value status = + b.create(loc, gas, + /*address=*/bExt.genI256Const(precompileAddr), + /*inpOffset=*/dataSlot, dataLen, + /*outOffset=*/zero, /*outSize=*/retSize); -struct BareCallOpLowering : public OpConversionPattern { - BareCallOpLowering(TypeConverter &tyConv, MLIRContext *ctx, - evm::StringLiteralPool &literals) - : OpConversionPattern(tyConv, ctx), literals(literals) {} - - LogicalResult matchAndRewrite(sol::BareCallOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - return lowerBareCallLikeOp( - op, adaptor, r, literals, - [](Location loc, auto adaptor, Value gas, Value addr, Value inpStart, - Value inpSize, Value zero, ConversionPatternRewriter &r) { - return r.create(loc, gas, addr, adaptor.getVal(), - /*inpOffset=*/inpStart, inpSize, - /*outOffset=*/zero, - /*outSize=*/zero); - }); - } + auto statusIsZero = bExt.genCmp(yul::CmpPredicate::eq, status, zero); + evmB.genForwardingRevert(statusIsZero); - evm::StringLiteralPool &literals; -}; + auto res = evmB.genLoad(zero, dataLoc); + // A ripemd160 result is left-aligned. + if constexpr (std::is_same_v) + res = b.create(loc, bExt.genI256Const(96), res); + return res; +} -struct BareDelegateCallOpLowering - : public OpConversionPattern { - BareDelegateCallOpLowering(TypeConverter &tyConv, MLIRContext *ctx, - evm::StringLiteralPool &literals) - : OpConversionPattern(tyConv, ctx), literals(literals) {} - - LogicalResult matchAndRewrite(sol::BareDelegateCallOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - return lowerBareCallLikeOp( - op, adaptor, r, literals, - [](Location loc, auto adaptor, Value gas, Value addr, Value inpStart, - Value inpSize, Value zero, ConversionPatternRewriter &r) { - return r.create(loc, gas, addr, - /*inpOffset=*/inpStart, inpSize, - /*outOffset=*/zero, - /*outSize=*/zero); - }); - } +Value Lowering::lower(sol::EcrecoverOp op) { + Location loc = op.getLoc(); + solgen::BuilderExt bExt(b, loc); + evm::Builder evmB = evmBuilder(loc); - evm::StringLiteralPool &literals; -}; + Value zero = bExt.genI256Const(0); + Value retSize = bExt.genI256Const(32); + Value freePtr = evmB.genFreePtr(); + Value paramsSize = bExt.genI256Const(128); + // The precompile reads the input only during the call, so the free pointer + // does not move. + evmB.genABIEncoding(op.getOperandTypes(), lowered(op.getOperands()), freePtr); + + Value gas = b.create(loc); + // The output area must be cleared before the call: a failing ecrecover + // cannot be detected from the status (the precompile succeeds with empty + // returndata, leaving the output area untouched), so this zero is what + // the caller reads back as the documented address(0) failure result. + b.create(loc, zero, zero); + Value status = + b.create(loc, gas, + /*address=*/bExt.genI256Const(1), + /*inpOffset=*/freePtr, paramsSize, + /*outOffset=*/zero, /*outSize=*/retSize); -struct BareStaticCallOpLowering - : public OpConversionPattern { - BareStaticCallOpLowering(TypeConverter &tyConv, MLIRContext *ctx, - evm::StringLiteralPool &literals) - : OpConversionPattern(tyConv, ctx), literals(literals) {} - - LogicalResult matchAndRewrite(sol::BareStaticCallOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - return lowerBareCallLikeOp( - op, adaptor, r, literals, - [](Location loc, auto adaptor, Value gas, Value addr, Value inpStart, - Value inpSize, Value zero, ConversionPatternRewriter &r) { - return r.create(loc, gas, addr, - /*inpOffset=*/inpStart, inpSize, - /*outOffset=*/zero, - /*outSize=*/zero); - }); - } + auto statusIsZero = bExt.genCmp(yul::CmpPredicate::eq, status, zero); + evmB.genForwardingRevert(statusIsZero); + return evmB.genLoad(zero, sol::DataLocation::Memory); +} - evm::StringLiteralPool &literals; -}; +//===----------------------------------------------------------------------===// +// Environment +//===----------------------------------------------------------------------===// -/// Lowers the common value-transfer call pattern used by 'sol.send' and -/// 'sol.transfer' and returns the requested status predicate. -template -static Value genValueTransferStatus(OpT op, AdaptorT adaptor, - yul::CmpPredicate pred, - ConversionPatternRewriter &r) { +Value Lowering::lower(sol::SigOp op) { Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - evm::Builder evmB(getModule(op), r, loc); - Value zero = bExt.genI256Const(0); - Value callStipend = bExt.genI256Const(2300); - Value valueIsZero = - bExt.genCmp(yul::CmpPredicate::eq, adaptor.getVal(), zero); - Value gas = r.create(loc, valueIsZero, callStipend, zero); - // old codegen begin - // (Via-IR cleans on casts, not here). - Value addr = evmB.genCleanup(op.getAddr().getType(), adaptor.getAddr(), loc); - // old codegen end - - Value callStatus = r.create(loc, gas, addr, adaptor.getVal(), - /*inpOffset=*/zero, /*inpSize=*/zero, - /*outOffset=*/zero, - /*outSize=*/zero); - return bExt.genCmp(pred, callStatus, zero); + solgen::BuilderExt bExt(b, loc); + Value data = b.create(loc, bExt.genI256Const(0)); + // Mask to keep only the top 4 bytes (bytes4 is MSB-aligned). + Value mask = bExt.genI256Const(llvm::APInt::getHighBitsSet(256, 32)); + return b.create(loc, data, mask); } -struct SendOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; +Value Lowering::lower(sol::BlockHashOp op) { + return b.create(op.getLoc(), lowered(op.getBlockNumber())); +} - LogicalResult matchAndRewrite(sol::SendOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Value status = - genValueTransferStatus(op, adaptor, yul::CmpPredicate::ne, r); - r.replaceOp(op, status); - return success(); - } -}; +Value Lowering::lower(sol::BlobHashOp op) { + return b.create(op.getLoc(), lowered(op.getIdx())); +} -struct TransferOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; +void Lowering::lower(sol::SelfdestructOp op) { + b.create(op.getLoc(), getCleaned(op)[0]); +} - LogicalResult matchAndRewrite(sol::TransferOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - evm::Builder evmB(getModule(op), r, loc); +Value Lowering::lower(sol::LibAddrOp op) { + return b.create(op.getLoc(), op.getName()); +} - Value statusIsZero = - genValueTransferStatus(op, adaptor, yul::CmpPredicate::eq, r); - evmB.genForwardingRevert(statusIsZero); - r.eraseOp(op); - return success(); - } -}; +// old codegen begin +// (Via-IR cleans on casts, not here). +Value Lowering::lower(sol::CodeHashOp op) { + return b.create(op.getLoc(), getCleaned(op)[0]); +} -struct ExtICallOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::ExtICallOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - - Value callee = adaptor.getCallee(); - auto i256Ty = r.getIntegerType(256); - Value addr = r.create(loc, i256Ty, callee, - r.getDenseI64ArrayAttr({0})); - Value selector = r.create( - loc, i256Ty, callee, r.getDenseI64ArrayAttr({1})); - - // Get callee function type from the ext_func_ref type. - auto extFuncRefTy = cast(op.getCallee().getType()); - auto calleeType = extFuncRefTy.getFuncTy(); - - // Lower to ExtCallOp - reuse all its lowering logic. - // Both ExtICallOp and ExtCallOp return (i1 status, results...). - auto extCall = r.create(loc, op.getResultTypes(), - /*callee=*/"", - /*ins=*/op.getCalleeOperands(), - /*addr=*/addr, - /*gas=*/adaptor.getGas(), - /*val=*/adaptor.getValue(), - /*selector=*/selector, - /*try_call=*/op.getTryCall(), - /*static_call=*/op.getStaticCall(), - /*delegate_call=*/false, - /*library_call=*/false, - /*callee_type=*/calleeType, - /*arg_attrs=*/ArrayAttr{}, - /*res_attrs=*/ArrayAttr{}); - - r.replaceOp(op, extCall.getResults()); - return success(); - } -}; +Value Lowering::lower(sol::BalanceOp op) { + return b.create(op.getLoc(), getCleaned(op)[0]); +} +// old codegen end -struct NewOpLowering : public OpConversionPattern { - NewOpLowering(TypeConverter &tyConv, MLIRContext *ctx, - evm::StringLiteralPool &literals) - : OpConversionPattern(tyConv, ctx), literals(literals) {} - - LogicalResult matchAndRewrite(sol::NewOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - evm::Builder evmB(getModule(op), r, loc, literals); - - Value bytecodeAddr = evmB.genFreePtr(); - Value dataOffset = r.create(loc, op.getObjName()); - Value dataSize = r.create(loc, op.getObjName()); - r.create(loc, bytecodeAddr, dataOffset, dataSize); - - Value tupleStart = r.create(loc, bytecodeAddr, dataSize); - Value tupleEnd = evmB.genABIEncoding(op.getCtorArgs().getType(), - adaptor.getCtorArgs(), tupleStart); - Value allocSize = r.create(loc, tupleEnd, bytecodeAddr); - - Value status; - if (op.getSalt()) - status = r.create(loc, adaptor.getVal(), bytecodeAddr, - allocSize, adaptor.getSalt()); - else - status = r.create(loc, adaptor.getVal(), bytecodeAddr, - allocSize); - - Value addr = status; - if (!op.getTryCall()) { - Value zero = bExt.genI256Const(0); - Value addrIsZero = bExt.genCmp(yul::CmpPredicate::eq, addr, zero); - evmB.genForwardingRevert(addrIsZero); - } - r.replaceOp(op, addr); - return success(); - } +void Lowering::lower(sol::AssertOp op) { + Location loc = op.getLoc(); + solgen::BuilderExt bExt(b, loc); + Value negCond = bExt.genCmp(yul::CmpPredicate::eq, lowered(op.getCond()), + bExt.genBool(false)); + evmBuilder(loc).genPanic(evm::PanicCode::Assert, negCond); +} - evm::StringLiteralPool &literals; -}; +void Lowering::lower(sol::RequireOp op) { + Location loc = op.getLoc(); + solgen::BuilderExt bExt(b, loc); + evm::Builder evmB = evmBuilder(loc); + Value falseVal = bExt.genBool(false); + Value negCond = + bExt.genCmp(yul::CmpPredicate::eq, lowered(op.getCond()), falseVal); + if (op.getCall()) { + assert(op.getMsg()); + evmB.genRevert(negCond, op.getArgs().getTypes(), lowered(op.getArgs()), + *op.getMsg()); + return; + } + if (op.getMsg()) + evmB.genUserRevertWithMsg(negCond, op.getMsg()->str()); + else + evmB.genRevert(negCond); +} -struct ObjectCodeOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::ObjectCodeOp op, OpAdaptor /*adaptor*/, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - evm::Builder evmB(getModule(op), r, loc); - - Value dataSize = r.create(loc, op.getObjName()); - Value alloc = evmB.genMemAlloc(op.getType(), /*zeroInit=*/false, - /*initVals=*/{}, dataSize, Type{}); - Value dataAddr = evmB.genDataAddrPtr(alloc, sol::DataLocation::Memory); - Value dataOffset = r.create(loc, op.getObjName()); - r.create(loc, dataAddr, dataOffset, dataSize); - r.replaceOp(op, alloc); - return success(); - } -}; +//===----------------------------------------------------------------------===// +// Control flow +//===----------------------------------------------------------------------===// -// old codegen begin -// (Via-IR cleans on casts, not here). -struct CodeOpLowering - : public CleanedOperandsLowering { - using Base = CleanedOperandsLowering; - using Base::Base; - - LogicalResult rewriteCleaned(sol::CodeOp op, ConversionPatternRewriter &r, - Value addr) const { - - Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - evm::Builder evmB(getModule(op), r, loc); - - auto extCodeSize = r.create(loc, addr); - Value alloc = evmB.genFreePtr(); - r.create(loc, alloc, extCodeSize); - auto codeAddr = evmB.genDataAddrPtr(alloc, sol::DataLocation::Memory); - r.create(loc, addr, /*dstOffset=*/codeAddr, - /*srcOffset=*/bExt.genI256Const(0), - extCodeSize); - evmB.genFreePtrStore(r.create(loc, codeAddr, extCodeSize)); - r.replaceOp(op, alloc); - return success(); - } -}; -// old codegen end +void Lowering::lower(sol::IfOp op) { + assert(op->getResults().empty() && + "Yul structured control flow does not support results yet"); + auto yulIf = + b.create(op.getLoc(), TypeRange{}, lowered(op.getCond())); + lowerRegions(op, yulIf); +} -struct CodeSizeOpLowering - : public CleanedOperandsLowering { - using Base = CleanedOperandsLowering; - using Base::Base; +void Lowering::lower(sol::SwitchOp op) { + auto yulSwitch = + b.create(op.getLoc(), TypeRange{}, lowered(op.getArg()), + op.getCases(), op.getCaseRegions().size()); + lowerRegions(op, yulSwitch); +} - LogicalResult rewriteCleaned(sol::CodeSizeOp op, ConversionPatternRewriter &r, - Value addr) const { - r.replaceOpWithNewOp(op, addr); - return success(); - } -}; +void Lowering::lower(sol::WhileOp op) { + lowerRegions(op, + b.create(op.getLoc(), TypeRange{}, ValueRange{})); +} -struct TryOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; +void Lowering::lower(sol::DoWhileOp op) { + lowerRegions(op, b.create(op.getLoc())); +} - LogicalResult matchAndRewrite(sol::TryOp tryOp, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = tryOp.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); +void Lowering::lower(sol::ForOp op) { + lowerRegions(op, + b.create(op.getLoc(), TypeRange{}, ValueRange{})); +} - auto ifStatus = r.create(loc, tryOp.getStatus()); +void Lowering::lower(sol::ScopeOp op) { + lowerRegions(op, b.create(op.getLoc())); +} - // - // Success region - // +void Lowering::lower(sol::LeaveOp op) { + b.create(op.getLoc()); +} - if (tryOp.getSuccessRegion().empty()) { - r.setInsertionPointToStart(&tryOp.getSuccessRegion().emplaceBlock()); - r.create(loc); - } else { - r.inlineRegionBefore(tryOp.getSuccessRegion(), ifStatus.getThenRegion(), - ifStatus.getThenRegion().begin()); - } +void Lowering::lower(sol::YieldOp op) { + assert(op.getIns().empty() && + "Yul structured control flow does not support yielded values yet"); + b.create(op.getLoc()); +} - // - // Failure region - // +void Lowering::lower(sol::ConditionOp op) { + b.create(op.getLoc(), lowered(op.getCondition()), + ValueRange{}); +} - if (tryOp.getFallbackRegion().empty()) { - evm::Builder evmB(getModule(tryOp), r, loc); - r.setInsertionPointToStart(&ifStatus.getElseRegion().emplaceBlock()); - evmB.genForwardingRevert(); - } else { - r.inlineRegionBefore(tryOp.getFallbackRegion(), ifStatus.getElseRegion(), - ifStatus.getElseRegion().begin()); - // If the fallback clause declared a parameter, materialize the raw return - // data as a `bytes memory` and replace uses of the block argument. - Block &entry = ifStatus.getElseRegion().front(); - if (entry.getNumArguments() > 0) { - BlockArgument blkArg = entry.getArgument(0); - Location argLoc = blkArg.getLoc(); - OpBuilder::InsertionGuard g(r); - r.setInsertionPointToStart(&entry); - evm::Builder evmB(getModule(tryOp), r, argLoc); - Value buf = evmB.genReturnDataBytes(argLoc); - Value repl = getTypeConverter()->materializeSourceConversion( - r, argLoc, blkArg.getType(), buf); - blkArg.replaceAllUsesWith(repl); - entry.eraseArgument(0); - } - } +void Lowering::lower(sol::BreakOp op) { b.create(op.getLoc()); } - if (tryOp.getPanicRegion().empty() && tryOp.getErrorRegion().empty()) { - r.eraseOp(tryOp); - return success(); - } +void Lowering::lower(sol::ContinueOp op) { + b.create(op.getLoc()); +} - r.setInsertionPointToStart(&ifStatus.getElseRegion().front()); +//===----------------------------------------------------------------------===// +// Functions +//===----------------------------------------------------------------------===// - // Generate a flag to check if we need to run the fallback. The flag will be - // set to false by any of the other clause. - auto boolAllocaTy = sol::PointerType::get(r.getContext(), r.getI1Type(), - sol::DataLocation::Stack); - auto runFallbackFlag = r.create(loc, boolAllocaTy); - r.create(loc, bExt.genBool(true), runFallbackFlag); +SmallVector Lowering::lower(sol::CallOp op) { + return genCall(op.getCalleeAttr().getAttr(), lowered(op.getOperands()), + op.getLoc()) + .getResults(); +} - // Generate an if op that checks if the returndata is large enough to hold - // the selector. - // - // The ops after this if op belong to the fallback. Track it for the - // fallback lowering. - auto returnDataSize = r.create(loc); - auto selectorRetCond = bExt.genCmp(yul::CmpPredicate::ugt, returnDataSize, - bExt.genI256Const(3)); - auto ifSelectorRet = r.create(loc, selectorRetCond); - auto fallbackPoint = r.saveInsertionPoint().getPoint(); +void Lowering::lower(sol::ReturnOp op) { + b.create(op.getLoc(), lowered(op.getOperands())); +} - // - // Selector region - // - r.setInsertionPointToStart(&ifSelectorRet.getThenRegion().emplaceBlock()); - r.setInsertionPoint(r.create(loc)); +//===----------------------------------------------------------------------===// +// Memory and storage +//===----------------------------------------------------------------------===// - // Generate the selector extraction code. - auto zero = bExt.genI256Const(0); - r.create(loc, /*dst=*/zero, /*src=*/zero, - /*size=*/bExt.genI256Const(4)); - auto selectorWord = r.create(loc, zero); - auto selector = - r.create(loc, selectorWord, bExt.genI256Const(224)); - - // Generate the switch for the panic and error catch blocks. - SmallVector switchSelectors; - APInt panicSelector( - /*numBits=*/256, mlir::evm::selectorFromSignature("Panic(uint256)")); - APInt errorSelector( - /*numBits=*/256, mlir::evm::selectorFromSignature("Error(string)")); - if (!tryOp.getPanicRegion().empty()) - switchSelectors.push_back(panicSelector); - if (!tryOp.getErrorRegion().empty()) - switchSelectors.push_back(errorSelector); - assert(!switchSelectors.empty()); - auto switchSelectorsAttr = mlir::DenseIntElementsAttr::get( - RankedTensorType::get(static_cast(switchSelectors.size()), - r.getIntegerType(256)), - switchSelectors); - auto switchOp = r.create(loc, selector, switchSelectorsAttr, - switchSelectors.size()); - r.setInsertionPointToStart(&switchOp.getDefaultRegion().emplaceBlock()); - r.setInsertionPoint(r.create(loc)); +Value Lowering::lower(sol::AllocaOp op) { + auto ptrTy = cast(op.getAllocType()); + return b.create( + op.getLoc(), LLVM::LLVMPointerType::get(b.getContext()), + evm::getYulType(ptrTy.getPointeeType()), + solgen::BuilderExt(b, op.getLoc()).genI256Const(1)); +} - // - // Panic case region - // - if (!tryOp.getPanicRegion().empty()) { - // FIXME: We should query for the case region using the attribute! - r.setInsertionPointToStart(&switchOp.getCaseRegions()[0].emplaceBlock()); - r.setInsertionPoint(r.create(loc)); - - // Genereate an if op that checks if the returndata is large enough to - // hold the panic code. - auto panicRetCond = bExt.genCmp(yul::CmpPredicate::ugt, returnDataSize, - bExt.genI256Const(0x23)); - auto ifPanicRet = r.create(loc, panicRetCond); - - // Inline the panic region. - r.inlineRegionBefore(tryOp.getPanicRegion(), ifPanicRet.getThenRegion(), - ifPanicRet.getThenRegion().begin()); - Block &thenEntry = ifPanicRet.getThenRegion().front(); - r.setInsertionPointToStart(&thenEntry); - r.create(loc, bExt.genBool(false), runFallbackFlag); - // Replace the panic code block arg with the panic code at offset 4. - BlockArgument blkArg = thenEntry.getArgument(0); - r.create(blkArg.getLoc(), /*dst=*/zero, - /*src=*/bExt.genI256Const(4), - /*size=*/bExt.genI256Const(0x20)); - Value panicCode = r.create(blkArg.getLoc(), zero); - auto blkArgRepl = getTypeConverter()->materializeSourceConversion( - r, loc, blkArg.getType(), panicCode); - // FIXME: Why does the following cause a "no matched legalization pattern" - // of the op from the materialization? Is this related to early - // legalization of in-place modifications? - // (https://discourse.llvm.org/t/dialect-conversion-fails-if-some-requires-recursive-application/79371/6) - // r.replaceAllUsesWith(blkArg, blkArgRepl); - blkArg.replaceAllUsesWith(blkArgRepl); - thenEntry.eraseArgument(0); - } +Value Lowering::lower(sol::MallocOp op) { + Type sizeTy = op.getSize() ? op.getSize().getType() : Type{}; + Value size = op.getSize() ? lowered(op.getSize()) : Value{}; + return evmBuilder(op.getLoc()) + .genMemAlloc(op.getType(), op.getZeroInit(), {}, size, sizeTy); +} - // - // Error case region - // - if (!tryOp.getErrorRegion().empty()) { - // FIXME: We should query for the case region using the attribute! - unsigned errorCaseIdx = switchSelectors.size() == 2 ? 1 : 0; - r.setInsertionPointToStart( - &switchOp.getCaseRegions()[errorCaseIdx].emplaceBlock()); - r.setInsertionPoint(r.create(loc)); - - evm::Builder evmB(getModule(tryOp), r, loc); - - // Decode the ABI-encoded Error(string) like upstream's - // try_decode_error_message. Three nested guards; a failed one leaves - // runFallbackFlag set so the fallback clause runs: - // - // - 0x44 = 4 (selector) + 0x20 (offset) + 0x20 (length) - // - 0x24 = 4 (selector) + 0x20 (offset) - // - offset is the ABI offset to the string body, read from the head. - // - length is the string length, read at msg. - // - dataEnd is one past the last byte of the returned tuple. - // - // clang-format off - // - // if (returndatasize >= 0x44) - // if (offset <= u64Max && offset + 0x24 <= returndatasize) - // if (length <= u64Max && msg + 0x20 + length <= dataEnd) - // run the Error clause - // - // clang-format on - Value sizeOk = bExt.genCmp(yul::CmpPredicate::uge, returnDataSize, - bExt.genI256Const(0x44)); - auto ifSizeOk = r.create(loc, sizeOk); - r.setInsertionPointToStart(&ifSizeOk.getThenRegion().emplaceBlock()); - r.setInsertionPoint(r.create(loc)); - Value u64Max = bExt.genI256Const(APInt::getLowBitsSet(256, 64)); - Value abiTupleSize = - r.create(loc, returnDataSize, bExt.genI256Const(4)); - Value abiTuple = evmB.genFreePtr(); - r.create(loc, /*dst=*/abiTuple, - /*src=*/bExt.genI256Const(4), - abiTupleSize); - Value offset = r.create(loc, abiTuple); - Value offsetSmall = bExt.genCmp(yul::CmpPredicate::ule, offset, u64Max); - Value offsetEnd = - r.create(loc, offset, bExt.genI256Const(0x24)); - Value offsetInBounds = - bExt.genCmp(yul::CmpPredicate::ule, offsetEnd, returnDataSize); - - Value offsetOk = r.create(loc, offsetSmall, offsetInBounds); - auto ifOffsetOk = r.create(loc, offsetOk); - r.setInsertionPointToStart(&ifOffsetOk.getThenRegion().emplaceBlock()); - r.setInsertionPoint(r.create(loc)); - - Value errMsg = r.create(loc, abiTuple, offset); - Value length = r.create(loc, errMsg); - Value lengthSmall = bExt.genCmp(yul::CmpPredicate::ule, length, u64Max); - Value msgEnd = r.create( - loc, r.create(loc, errMsg, bExt.genI256Const(0x20)), - length); - Value dataEnd = r.create(loc, abiTuple, abiTupleSize); - Value lengthInBounds = - bExt.genCmp(yul::CmpPredicate::ule, msgEnd, dataEnd); - Value lengthOk = r.create(loc, lengthSmall, lengthInBounds); - auto ifLengthOk = r.create(loc, lengthOk); - r.inlineRegionBefore(tryOp.getErrorRegion(), ifLengthOk.getThenRegion(), - ifLengthOk.getThenRegion().begin()); - Block &thenEntry = ifLengthOk.getThenRegion().front(); - r.setInsertionPointToStart(&thenEntry); - r.create(loc, bExt.genBool(false), runFallbackFlag); - // The copy is reserved only once every guard passed, and only up to the - // end of the reason. - evmB.genFreePtrUpd(abiTuple, r.create(loc, msgEnd, abiTuple)); - BlockArgument blkArg = thenEntry.getArgument(0); - auto blkArgRepl = getTypeConverter()->materializeSourceConversion( - r, loc, blkArg.getType(), errMsg); - blkArg.replaceAllUsesWith(blkArgRepl); - thenEntry.eraseArgument(0); - } +Value Lowering::lower(sol::ArrayLitOp op) { + return evmBuilder(op.getLoc()) + .genMemAlloc(op.getType(), false, lowered(op.getIns()), {}, Type{}); +} - // - // Fallback region - // - Block *fallbackBlk = r.splitBlock(fallbackPoint->getBlock(), fallbackPoint); - r.setInsertionPointAfter(ifSelectorRet); - auto runFallbackFlagLd = r.create(loc, runFallbackFlag); - auto ifRunFallback = r.create(loc, runFallbackFlagLd); - ifRunFallback.getThenRegion().emplaceBlock(); - r.inlineBlockBefore(fallbackBlk, &ifRunFallback.getThenRegion().front(), - ifRunFallback.getThenRegion().front().begin()); - r.create(loc); - - r.eraseOp(tryOp); - return success(); - } -}; +Value Lowering::lower(sol::StringLitOp op) { + Location loc = op.getLoc(); + evm::Builder evmB = evmBuilder(loc); + solgen::BuilderExt bExt(b, loc); + StringRef lit = op.getValue(); + size_t roundedLen = llvm::alignTo(lit.size(), 32u); + Value allocPtr = evmB.genMemAlloc(bExt.genI256Const(32 + roundedLen), loc); + b.create(loc, allocPtr, bExt.genI256Const(lit.size())); + evmB.genStringLiteralData( + lit, evmB.genDataAddrPtr(allocPtr, sol::DataLocation::Memory, loc), loc); + return allocPtr; +} -struct AssertOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; +Value Lowering::lower(sol::ConcatOp op) { + Location loc = op.getLoc(); + evm::Builder evmB = evmBuilder(loc); + solgen::BuilderExt bExt(b, loc); - LogicalResult matchAndRewrite(sol::AssertOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - evm::Builder evmB(getModule(op), r, loc); + Value freePtr = evmB.genFreePtr(); + Value dstStart = b.create(loc, freePtr, bExt.genI256Const(32)); - // Generate: if (!cond) { panic(0x01) } - mlir::Value falseVal = bExt.genBool(false); - mlir::Value negCond = - bExt.genCmp(yul::CmpPredicate::eq, adaptor.getCond(), falseVal); - evmB.genPanic(evm::PanicCode::Assert, negCond); + Value currDst = dstStart; + for (Value arg : op.getArgs()) { + Type ty = arg.getType(); + if (isa(ty)) { + StringRef bytes = + cast(arg.getDefiningOp()).getValue(); + evmB.genStringLiteralData(bytes, currDst, loc); + currDst = + b.create(loc, currDst, bExt.genI256Const(bytes.size())); + continue; + } + + if (auto bytesTy = dyn_cast(ty)) { + // The bytes are cleaned so that the padding is zero. + b.create(loc, currDst, + evmB.genCleanup(ty, lowered(arg), loc)); + Value length = bExt.genI256Const(bytesTy.getSize()); + currDst = b.create(loc, currDst, length); + continue; + } + + assert(isa(ty)); + Value length = + evmB.genCopyStringDataToMemory(lowered(arg), ty, currDst, loc); + currDst = b.create(loc, currDst, length); + } + Value dataSize = b.create(loc, currDst, dstStart); + b.create(loc, freePtr, dataSize); + evmB.genFreePtrStore(currDst); + return freePtr; +} - r.eraseOp(op); - return success(); +Value Lowering::lower(sol::GetCallDataOp op) { + Location loc = op.getLoc(); + solgen::BuilderExt bExt(b, loc); + Value zero = bExt.genI256Const(0); + if (auto strTy = dyn_cast(op.getType()); + strTy && strTy.getDataLocation() == sol::DataLocation::CallData) { + Value callDataSz = b.create(loc); + return bExt.genLLVMStruct({zero, callDataSz}); } -}; + return zero; +} -struct RequireOpLowering : public OpConversionPattern { - RequireOpLowering(MLIRContext *ctx, evm::StringLiteralPool &literals) - : OpConversionPattern(ctx), literals(literals) {} - - LogicalResult matchAndRewrite(sol::RequireOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - evm::Builder evmB(getModule(op), r, loc, literals); - - // Generate the revert condition. - mlir::Value falseVal = bExt.genBool(false); - mlir::Value negCond = - bExt.genCmp(yul::CmpPredicate::eq, adaptor.getCond(), falseVal); - if (op.getCall()) { - assert(op.getMsg()); - evmB.genRevert(negCond, op.getArgs().getTypes(), adaptor.getArgs(), - *op.getMsg()); - r.eraseOp(op); - return success(); - } +Value Lowering::lower(sol::DefaultCallDataOp op) { + Location loc = op.getLoc(); + solgen::BuilderExt bExt(b, loc); + Value zero = bExt.genI256Const(0); + Value callDataSz = b.create(loc); + // A slice defaults to the empty {calldatasize(), 0}, any other reference to + // calldatasize(), an offset past valid calldata. + if (isa(evm::getYulType(op.getType()))) + return bExt.genLLVMStruct({callDataSz, zero}); + return callDataSz; +} - // Generate the revert. - if (op.getMsg()) - evmB.genUserRevertWithMsg(negCond, op.getMsg()->str()); - else - evmB.genRevert(negCond); +// A storage or transient reference is a slot, and slot 0 is the default. +Value Lowering::lower(sol::DefaultStorageOp op) { + return solgen::BuilderExt(b, op.getLoc()).genI256Const(0); +} - r.eraseOp(op); - return success(); - } +Value Lowering::lower(sol::PushOp op) { + Location loc = op.getLoc(); + evm::Builder evmB = evmBuilder(loc); + solgen::BuilderExt bExt(b, loc); + Type inpTy = op.getInp().getType(); + // The address of the new element: a {slot, byte offset} pair for a packed + // element, one word otherwise. + Type resTy = evm::getYulType(op.getType()); + + if (isa(inpTy)) + return evmB.genHelperCall( + evm::helpersym::pushStorageString(/*zero=*/true), + {lowered(op.getInp())}, resTy, + [&](ValueRange args, Location bodyLoc) { + return evmB.genPushVoidToString(args[0], bodyLoc); + }, + loc); - evm::StringLiteralPool &literals; -}; + auto arrTy = cast(inpTy); + return evmB.genHelperCall( + evm::helpersym::pushStorageArray(arrTy), {lowered(op.getInp())}, resTy, + [&](ValueRange args, Location bodyLoc) { + Value slot = args[0]; + Value oldSize = b.create(bodyLoc, slot); + Value newSize = b.create(bodyLoc, oldSize, + bExt.genI256Const(1, bodyLoc)); + b.create(bodyLoc, slot, newSize); + Value dataSlot = + evmB.genDataAddrPtr(slot, sol::DataLocation::Storage, bodyLoc); + return evmB.genStorageEltAddr(dataSlot, oldSize, arrTy, bodyLoc); + }); +} -struct EmitOpLowering : public OpConversionPattern { - EmitOpLowering(TypeConverter &tyConv, MLIRContext *ctx, - evm::StringLiteralPool &literals) - : OpConversionPattern(tyConv, ctx), literals(literals) {} +void Lowering::lower(sol::PushStringOp op) { + Location loc = op.getLoc(); + evm::Builder evmB = evmBuilder(loc); + solgen::BuilderExt bExt(b, loc); + Value val = lowered(op.getValue()); + Value byte = isa(op.getValue().getType()) + ? b.create(loc, val, bExt.genI256Const(248)) + .getResult() + : val; + // The helper takes the string slot and the byte, right-aligned. + evmB.genHelperCall( + evm::helpersym::pushStorageString(/*zero=*/false), + {lowered(op.getAddr()), byte}, + [&](ValueRange args, Location bodyLoc) { + evmB.genPushToString(args[0], args[1], bodyLoc); + }, + loc); +} - LogicalResult matchAndRewrite(sol::EmitOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = op.getLoc(); - mlir::solgen::BuilderExt bExt(r, loc); - evm::Builder evmB(getModule(op), r, loc, literals); +void Lowering::lower(sol::PopOp op) { + Location loc = op.getLoc(); + evm::Builder evmB = evmBuilder(loc); + solgen::BuilderExt bExt(b, loc); + Type inpTy = op.getInp().getType(); + Value slot = lowered(op.getInp()); - // Collect the remapped indexed and non-indexed args. - // - // FIXME: How do we get `extraClassDeclaration` functions to be part of the - // OpAdaptor? - auto remappedOperands = adaptor.getOperands(); - std::vector indexedArgs, nonIndexedArgs; - std::vector nonIndexedArgsType; - if (op.getSignature()) { - auto signatureHash = evm::keccak256AsAPInt(op.getSignature()->str()); - indexedArgs.push_back(bExt.genI256Const(signatureHash)); - } - unsigned argIdx = 0; - for (Value origIdxArg : op.getIndexedArgs()) { - Type origTy = origIdxArg.getType(); - Value val = remappedOperands[argIdx++]; - - // Constant string indexed arg: topic = keccak256 of its bytes. - if (isa(origTy)) { - StringRef bytes = - cast(origIdxArg.getDefiningOp()).getValue(); - indexedArgs.push_back(bExt.genI256Const(evm::keccak256AsAPInt(bytes))); - continue; - } + if (isa(inpTy)) { + evmB.genHelperCall( + evm::helpersym::popStorageString(), {slot}, + [&](ValueRange args, Location bodyLoc) { + Value slot = args[0]; + Value data = evmB.genLoad(slot, sol::DataLocation::Storage, bodyLoc); + Value oldSize = evmB.genStorageStringLength(data, bodyLoc); + Value panicCond = bExt.genCmp(yul::CmpPredicate::eq, oldSize, + bExt.genI256Const(0, bodyLoc), bodyLoc); + evmB.genPanic(mlir::evm::PanicCode::EmptyArrayPop, panicCond, + bodyLoc); + evmB.genPopString(slot, data, oldSize, bodyLoc); + }, + loc); + return; + } + + auto arrTy = cast(inpTy); + evmB.genHelperCall( + evm::helpersym::popStorageArray(arrTy), {slot}, + [&](ValueRange args, Location bodyLoc) { + Value slot = args[0]; + Value oldSize = evmB.genLoad(slot, sol::DataLocation::Storage, bodyLoc); + Value panicCond = bExt.genCmp(yul::CmpPredicate::eq, oldSize, + bExt.genI256Const(0, bodyLoc), bodyLoc); + evmB.genPanic(mlir::evm::PanicCode::EmptyArrayPop, panicCond, bodyLoc); + Value newSize = b.create(bodyLoc, oldSize, + bExt.genI256Const(1, bodyLoc)); + // The removed element is cleared before the length is written, the + // upstream order. + evmB.genClearStorageArrayTail(slot, arrTy, newSize, oldSize, + /*isDecrement=*/true, bodyLoc); + b.create(bodyLoc, slot, newSize); + }); +} - // Reference-type indexed arg: topic = keccak256(packed_encode(arg)). - if (isa(origTy)) { - Value scratchStart = evmB.genFreePtr(); - Value scratchEnd = evmB.genABIEncodingPacked( - TypeRange{origTy}, ValueRange{val}, scratchStart, loc); - Value scratchSize = r.create(loc, scratchEnd, scratchStart); - indexedArgs.push_back( - r.create(loc, scratchStart, scratchSize)); - continue; - } +Value Lowering::lower(sol::AddrOfOp op) { + solgen::BuilderExt bExt(b, op.getLoc()); + Operation *sym = contr.lookupSymbol(op.getVar()); + assert(sym); + if (auto stateVarOp = dyn_cast(sym)) { + Value slot = bExt.genI256Const(stateVarOp.getSlot()); + if (!sol::canBePacked(stateVarOp.getType())) + return slot; + Value offset = bExt.genI256Const(stateVarOp.getByteOffset()); + return bExt.genLLVMStruct({slot, offset}); + } + return bExt.genI256Const(evm::getImmutableAddr(cast(sym))); +} - // Value-type indexed arg: cleanup-and-widen to i256 (LogOp expects i256 - // topics). - Value topic = isa(origTy) - ? evmB.genExtFuncPack(val, /*inStorage=*/false, loc) - : evmB.genCleanup(origTy, val, loc); - indexedArgs.push_back(topic); - } +Value Lowering::lower(sol::GepOp op) { + Location loc = op.getLoc(); + solgen::BuilderExt bExt(b, loc); + evm::Builder evmB = evmBuilder(loc); + + Type baseAddrTy = op.getBaseAddr().getType(); + Value baseAddr = lowered(op.getBaseAddr()); + Value idx = lowered(op.getIdx()); + Value cleanedIdx; + auto getCleanedIdx = [&]() -> Value { + if (!cleanedIdx) + cleanedIdx = evmB.genCleanup(op.getIdx().getType(), idx, loc); + return cleanedIdx; + }; + auto genBoundsCheck = [&](Value cond, + std::optional locArg = std::nullopt) { + if (op.getNoPanicBounds()) + evmB.genRevert(cond, locArg); + else + evmB.genPanic(mlir::evm::PanicCode::ArrayOutOfBounds, cond, locArg); + }; + auto getInBoundsConstIdx = [&](sol::ArrayType arrTy) -> std::optional { + APInt constIdx; + if (arrTy.isDynSized() || + !matchPattern(getCleanedIdx(), m_ConstantInt(&constIdx)) || + constIdx.uge(arrTy.getSize())) + return std::nullopt; + return constIdx; + }; + sol::DataLocation dataLoc = sol::getDataLocation(baseAddrTy); + + switch (dataLoc) { + case sol::DataLocation::Stack: { + auto stkPtrTy = + LLVM::LLVMPointerType::get(b.getContext(), evm::AddrSpace_Stack); + return b.create(loc, /*resultType=*/stkPtrTy, + /*basePtrType=*/baseAddr.getType(), baseAddr, + idx); + } + + case sol::DataLocation::Storage: { + if (auto arrTy = dyn_cast(baseAddrTy)) { + [[maybe_unused]] Type eltTy = arrTy.getEltType(); + assert((isa(eltTy) || isa(eltTy) || + sol::isAddressLikeType(eltTy) || isa(eltTy) || + isa(eltTy) || isa(eltTy) || + sol::isNonPtrRefType(eltTy)) && + "NYI"); + + // Bounds check (always for dynamic, unless an in-bounds constant index + // for static). + bool needsBoundsCheck = !getInBoundsConstIdx(arrTy); + Value arrayIdx = getCleanedIdx(); + + // The element address at `slot` and `index`, built at `l`: the bounds + // check, the keccak256 of the data slot for a dynamic array, and the + // stride or packed-slot arithmetic. + auto genElementAddr = [&](Value slot, Value index, Location l) -> Value { + if (needsBoundsCheck) { + Value size = arrTy.isDynSized() + ? evmB.genDynSize(slot, baseAddrTy, l) + : bExt.genI256Const(arrTy.getSize(), l); + auto panicCond = bExt.genCmp(yul::CmpPredicate::uge, index, size, l); + genBoundsCheck(panicCond, l); + } + Value baseSlot = arrTy.isDynSized() + ? evmB.genDataAddrPtr(slot, baseAddrTy, l) + : slot; + return evmB.genStorageEltAddr(baseSlot, index, arrTy, l); + }; - for (Value arg : op.getNonIndexedArgs()) { - nonIndexedArgsType.push_back(arg.getType()); - nonIndexedArgs.push_back(remappedOperands[argIdx++]); + // A bounds-checked access goes through a helper shared per array type + // and bounds behaviour. + if (!needsBoundsCheck) + return genElementAddr(baseAddr, arrayIdx, loc); + return evmB.genHelperCall( + evm::helpersym::indexStorageArray(arrTy, op.getNoPanicBounds()), + {baseAddr, arrayIdx}, evm::getYulType(op.getType()), + [&](ValueRange args, Location bodyLoc) { + return genElementAddr(args[0], args[1], bodyLoc); + }); + } + + if (auto structTy = dyn_cast(baseAddrTy)) { + auto constIdx = cast(idx.getDefiningOp()); + uint64_t fieldIdx = constIdx.getValue().getZExtValue(); + ArrayRef memberTypes = structTy.getMemberTypes(); + assert(fieldIdx < memberTypes.size() && + "struct field index out of range"); + Type fieldTy = memberTypes[fieldIdx]; + auto [slotOffset, byteOffset] = structTy.getStorageMemberOffset(fieldIdx); + + // A packed field is {baseSlot + slotOffset, byteOffset}. + if (sol::canBePacked(fieldTy)) { + Value slot = + b.create(loc, baseAddr, bExt.genI256Const(slotOffset)); + return bExt.genLLVMStruct({slot, bExt.genI256Const(byteOffset)}); + } + return b.create(loc, baseAddr, bExt.genI256Const(slotOffset)); } - // Generate the tuple encoding for the non-indexed args. - // TODO: Are we sure we need an unbounded allocation here? - Value tupleStart = evmB.genFreePtr(); - Value tupleEnd = - evmB.genABIEncoding(nonIndexedArgsType, nonIndexedArgs, tupleStart); - Value tupleSize = r.create(loc, tupleEnd, tupleStart); + assert(isa(baseAddrTy) && "NYI"); + return evmB.genStringItemAddress(baseAddr, getCleanedIdx(), loc); + } - // Generate sol.log and replace sol.emit with it. - r.replaceOpWithNewOp(op, tupleStart, tupleSize, indexedArgs); + case sol::DataLocation::CallData: + case sol::DataLocation::Memory: { + if (auto arrTy = dyn_cast(baseAddrTy)) { + Value addrAtIdx; - return success(); - } + // Don't generate out-of-bounds check for in-bounds constant indexing of + // static arrays. + if (std::optional constIdx = getInBoundsConstIdx(arrTy)) { + APInt stride = evm::getArrayEltStride(arrTy); + addrAtIdx = b.create(loc, baseAddr, + bExt.genI256Const(*constIdx * stride)); + } else { + Value size; + if (arrTy.isDynSized()) + size = evmB.genDynSize(baseAddr, baseAddrTy); + else + size = bExt.genI256Const(arrTy.getSize()); - evm::StringLiteralPool &literals; -}; + auto panicCond = + bExt.genCmp(yul::CmpPredicate::uge, getCleanedIdx(), size); + genBoundsCheck(panicCond); -struct RevertOpLowering : public OpConversionPattern { - RevertOpLowering(TypeConverter &tyConv, MLIRContext *ctx, - evm::StringLiteralPool &literals) - : OpConversionPattern(tyConv, ctx), literals(literals) {} - - LogicalResult matchAndRewrite(sol::RevertOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - evm::Builder evmB(getModule(op), r, op.getLoc(), literals); - if (op.getCall()) { - // revert ErrorName(...) - assert(op.getSignature() && !op.getSignature()->empty()); - evmB.genRevert(op.getArgs().getTypes(), adaptor.getArgs(), - *op.getSignature()); - } else if (op.getSignature()) { - // revert("reason"). An empty reason still ABI-encodes Error(""). - evmB.genUserRevertWithMsg(op.getSignature()->str()); - } else { - // revert() - evmB.genRevert(op.getLoc()); + Value stride = bExt.genI256Const(evm::getArrayEltStride(arrTy)); + Value scaledIdx = b.create(loc, getCleanedIdx(), stride); + if (arrTy.isDynSized()) { + Value dataAddr = evmB.genDataAddrPtr(baseAddr, baseAddrTy); + addrAtIdx = b.create(loc, dataAddr, scaledIdx); + } else { + addrAtIdx = b.create(loc, baseAddr, scaledIdx); + } + } + + // A calldata element with dynamic content is reached through the ABI + // relative-offset word in the head area. + Type eltTy = arrTy.getEltType(); + if (dataLoc == sol::DataLocation::CallData && + sol::hasDynamicallySizedElt(eltTy)) { + Value outerDataBase = arrTy.isDynSized() + ? evmB.genDataAddrPtr(baseAddr, baseAddrTy) + : baseAddr; + addrAtIdx = evmB.genCalldataAccessRef(eltTy, outerDataBase, addrAtIdx, + /*isNonABI=*/true, loc); + } + return addrAtIdx; } - r.eraseOp(op); - return success(); - } - evm::StringLiteralPool &literals; -}; + if (auto structTy = dyn_cast(baseAddrTy)) { + auto idxConstOp = cast(idx.getDefiningOp()); + uint64_t fieldIdx = idxConstOp.getValue().getZExtValue(); -static void lowerSolRegionTerminatorsToYul(Region ®ion, - ConversionPatternRewriter &r) { - for (Block &block : region) { - Operation *terminator = block.getTerminator(); - if (auto yieldOp = dyn_cast(terminator)) { - assert(yieldOp.getIns().empty() && - "Yul structured control flow does not support yielded values yet"); - r.setInsertionPoint(yieldOp); - r.replaceOpWithNewOp(yieldOp); - } else if (auto conditionOp = dyn_cast(terminator)) { - r.setInsertionPoint(conditionOp); - Value cond = conditionOp.getCondition(); - if (Value remappedCond = r.getRemappedValue(cond)) - cond = remappedCond; - r.replaceOpWithNewOp(conditionOp, cond, ValueRange{}); - } else if (auto breakOp = dyn_cast(terminator)) { - r.setInsertionPoint(breakOp); - r.replaceOpWithNewOp(breakOp); - } else if (auto continueOp = dyn_cast(terminator)) { - r.setInsertionPoint(continueOp); - r.replaceOpWithNewOp(continueOp); + unsigned byteOffset = 0; + ArrayRef memberTypes = structTy.getMemberTypes(); + assert(fieldIdx < memberTypes.size() && + "struct field index out of range"); + if (dataLoc == sol::DataLocation::CallData) { + for (uint64_t i = 0; i < fieldIdx; ++i) + byteOffset += evm::getCallDataHeadSize(memberTypes[i]); + } else { + byteOffset = fieldIdx * 32; + } + Value res = + b.create(loc, baseAddr, bExt.genI256Const(byteOffset)); + + // A dynamic member of a calldata struct is reached through its ABI + // relative offset. + if (dataLoc == sol::DataLocation::CallData) { + Type memberTy = memberTypes[fieldIdx]; + if (sol::hasDynamicallySizedElt(memberTy)) + res = evmB.genCalldataAccessRef(memberTy, baseAddr, res, + /*isNonABI=*/true, loc); + } + return res; } - } -} -struct ScopeOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::ScopeOp op, OpAdaptor, - ConversionPatternRewriter &r) const override { - auto yulScopeOp = r.create(op.getLoc()); - r.inlineRegionBefore(op.getBodyRegion(), yulScopeOp.getBodyRegion(), - yulScopeOp.getBodyRegion().end()); - lowerSolRegionTerminatorsToYul(yulScopeOp.getBodyRegion(), r); - r.eraseOp(op); - return success(); + assert(isa(baseAddrTy)); + Value size = evmB.genDynSize(baseAddr, baseAddrTy); + auto panicCond = bExt.genCmp(yul::CmpPredicate::uge, getCleanedIdx(), size); + genBoundsCheck(panicCond); + Value dataAddr = evmB.genDataAddrPtr(baseAddr, baseAddrTy); + return b.create(loc, dataAddr, getCleanedIdx()); } -}; - -struct LeaveOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - LogicalResult matchAndRewrite(sol::LeaveOp op, OpAdaptor, - ConversionPatternRewriter &r) const override { - r.replaceOpWithNewOp(op); - return success(); + default: + llvm_unreachable("NYI"); } -}; +} -struct IfOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::IfOp ifOp, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - Location loc = ifOp.getLoc(); - assert(ifOp->getResults().empty() && - "Yul structured control flow does not support results yet"); - - auto yulIfOp = r.create(loc, TypeRange{}, adaptor.getCond()); - r.inlineRegionBefore(ifOp.getThenRegion(), yulIfOp.getThenRegion(), - yulIfOp.getThenRegion().end()); - lowerSolRegionTerminatorsToYul(yulIfOp.getThenRegion(), r); - r.inlineRegionBefore(ifOp.getElseRegion(), yulIfOp.getElseRegion(), - yulIfOp.getElseRegion().end()); - lowerSolRegionTerminatorsToYul(yulIfOp.getElseRegion(), r); - - r.eraseOp(ifOp); - return success(); +Value Lowering::lower(sol::MapOp op) { + Location loc = op.getLoc(); + solgen::BuilderExt bExt(b, loc); + evm::Builder evmB = evmBuilder(loc); + Value mapping = lowered(op.getMapping()); + // The mapping is a slot: the result of a sol.addr_of or a sol.map. + assert(cast(mapping.getType()).getWidth() == 256); + + Type keyTy = op.getKey().getType(); + Value slot; + if (isa(keyTy)) { + // keccak256(abi.encodePacked(rawBytes, slot)) + Value pos = evmB.genFreePtr(loc); + Value dataLen = + evmB.genCopyStringDataToMemory(lowered(op.getKey()), keyTy, pos, loc); + Value slotAddr = b.create(loc, pos, dataLen); + b.create(loc, slotAddr, mapping); + Value totalLen = + b.create(loc, dataLen, bExt.genI256Const(0x20)); + slot = b.create(loc, pos, totalLen); + } else { + // keccak256(key_word, slot_word), the value-type key aligned in its word. + auto zero = bExt.genI256Const(0); + Value key = evmB.genCleanup(keyTy, lowered(op.getKey()), loc); + b.create(loc, zero, key); + b.create(loc, bExt.genI256Const(0x20), mapping); + slot = b.create(loc, zero, bExt.genI256Const(0x40)); } -}; -struct SwitchOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; + if (auto ptrTy = dyn_cast(op.getType()); + ptrTy && ptrTy.getDataLocation() == sol::DataLocation::Storage && + sol::canBePacked(ptrTy.getPointeeType())) + return bExt.genLLVMStruct({slot, bExt.genI256Const(0)}); + return slot; +} - LogicalResult matchAndRewrite(sol::SwitchOp switchOp, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - auto yulSwitchOp = r.create( - switchOp.getLoc(), TypeRange{}, adaptor.getArg(), switchOp.getCases(), - switchOp.getCaseRegions().size()); +Value Lowering::lower(sol::LoadOp op) { + Location loc = op.getLoc(); + solgen::BuilderExt bExt(b, loc); + evm::Builder evmB = evmBuilder(loc); + + Value addr = lowered(op.getAddr()); + sol::DataLocation dataLoc = sol::getDataLocation(op.getAddr().getType()); + Type eltTy = cast(op.getAddr().getType()).getPointeeType(); + + switch (dataLoc) { + case sol::DataLocation::Stack: + return b.create(loc, evm::getYulType(eltTy), addr, + evm::getAlignment(addr)); + case sol::DataLocation::Immutable: + return evmB.genLoad(addr, dataLoc); + case sol::DataLocation::CallData: { + Value ld = evmB.genLoad(addr, dataLoc); + if (isa(eltTy)) { + evmB.genExtFuncWordValidation(ld, loc); + ld = evmB.genExtFuncUnpack(ld, /*inStorage=*/false, loc); + } else if (isa(eltTy) || isa(eltTy) || + sol::isAddressLikeType(eltTy) || sol::isBytesLikeType(eltTy)) { + ld = evmB.genCleanup(eltTy, ld, loc, dataLoc); + } + return ld; + } + case sol::DataLocation::Memory: { + Value ld = evmB.genLoad(addr, dataLoc); + // old codegen begin + // (Via-IR cleans every memory load). + if (isa(eltTy)) + ld = evmB.genCleanup(eltTy, ld, loc, dataLoc); + // old codegen end + if (isa(eltTy)) + ld = evmB.genExtFuncUnpack(ld, /*inStorage=*/false, loc); + return ld; + } + case sol::DataLocation::Storage: + case sol::DataLocation::Transient: { + if (!sol::canBePacked(eltTy)) + return evmB.genLoad(addr, dataLoc); + + // A packed value is at {slot, byte offset}: the slot shifted right by + // offset * 8, cleaned. + Value slot = genPairField(addr, 0, loc); + Value offset = genPairField(addr, 1, loc); + Value slotVal = evmB.genLoad(slot, dataLoc); + Value shiftBits = b.create(loc, offset, bExt.genI256Const(8)); + Value shifted = b.create(loc, slotVal, shiftBits); + return evmB.genCleanupPackedStorageValue(eltTy, shifted, loc); + } + } + llvm_unreachable("unexpected data location"); +} - r.inlineRegionBefore(switchOp.getDefaultRegion(), - yulSwitchOp.getDefaultRegion(), - yulSwitchOp.getDefaultRegion().end()); - lowerSolRegionTerminatorsToYul(yulSwitchOp.getDefaultRegion(), r); +Value Lowering::lower(sol::LoadImmutableOp op) { + return b.create(op.getLoc(), op.getName()); +} - for (auto [srcRegion, dstRegion] : - llvm::zip(switchOp.getCaseRegions(), yulSwitchOp.getCaseRegions())) { - r.inlineRegionBefore(srcRegion, dstRegion, dstRegion.end()); - lowerSolRegionTerminatorsToYul(dstRegion, r); +void Lowering::lower(sol::StoreOp op) { + Location loc = op.getLoc(); + solgen::BuilderExt bExt(b, loc); + evm::Builder evmB = evmBuilder(loc); + + Value val = lowered(op.getVal()); + Value addr = lowered(op.getAddr()); + sol::DataLocation dataLoc = sol::getDataLocation(op.getAddr().getType()); + Type eltTy = cast(op.getAddr().getType()).getPointeeType(); + + switch (dataLoc) { + case sol::DataLocation::Stack: + b.create(loc, val, addr, evm::getAlignment(addr)); + return; + case sol::DataLocation::Immutable: + case sol::DataLocation::Memory: { + if (isa(eltTy) && dataLoc == sol::DataLocation::Memory) { + auto byteVal = b.create(loc, bExt.genI256Const(0), val); + b.create(loc, addr, byteVal); + return; } - r.eraseOp(switchOp); - return success(); + if (isa(eltTy)) + val = evmB.genExtFuncPack(val, /*inStorage=*/false, loc); + else if (isa(eltTy) || isa(eltTy) || + sol::isAddressLikeType(eltTy) || isa(eltTy)) + val = evmB.genCleanup(eltTy, val, loc); + evmB.genStore(val, addr, dataLoc); + return; } -}; + case sol::DataLocation::Storage: + case sol::DataLocation::Transient: { + if (!sol::canBePacked(eltTy)) { + evmB.genStore(val, addr, dataLoc); + return; + } -// (Copied and modified from clangir). -struct LoopOpInterfaceLowering - : public OpInterfaceConversionPattern { - using OpInterfaceConversionPattern< - sol::LoopOpInterface>::OpInterfaceConversionPattern; - - LogicalResult matchAndRewrite(sol::LoopOpInterface op, ArrayRef, - ConversionPatternRewriter &r) const override { - Operation *rawOp = op.getOperation(); - if (auto forOp = dyn_cast(rawOp)) { - auto yulForOp = - r.create(forOp.getLoc(), TypeRange{}, ValueRange{}); - r.inlineRegionBefore(forOp.getCond(), yulForOp.getCond(), - yulForOp.getCond().end()); - lowerSolRegionTerminatorsToYul(yulForOp.getCond(), r); - r.inlineRegionBefore(forOp.getBody(), yulForOp.getBody(), - yulForOp.getBody().end()); - lowerSolRegionTerminatorsToYul(yulForOp.getBody(), r); - r.inlineRegionBefore(forOp.getStep(), yulForOp.getStep(), - yulForOp.getStep().end()); - lowerSolRegionTerminatorsToYul(yulForOp.getStep(), r); - } else if (auto whileOp = dyn_cast(rawOp)) { - auto yulWhileOp = - r.create(whileOp.getLoc(), TypeRange{}, ValueRange{}); - r.inlineRegionBefore(whileOp.getCond(), yulWhileOp.getCond(), - yulWhileOp.getCond().end()); - lowerSolRegionTerminatorsToYul(yulWhileOp.getCond(), r); - r.inlineRegionBefore(whileOp.getBody(), yulWhileOp.getBody(), - yulWhileOp.getBody().end()); - lowerSolRegionTerminatorsToYul(yulWhileOp.getBody(), r); - } else if (auto doWhileOp = dyn_cast(rawOp)) { - auto yulDoWhileOp = r.create(doWhileOp.getLoc()); - r.inlineRegionBefore(doWhileOp.getBody(), yulDoWhileOp.getBody(), - yulDoWhileOp.getBody().end()); - lowerSolRegionTerminatorsToYul(yulDoWhileOp.getBody(), r); - r.inlineRegionBefore(doWhileOp.getCond(), yulDoWhileOp.getCond(), - yulDoWhileOp.getCond().end()); - lowerSolRegionTerminatorsToYul(yulDoWhileOp.getCond(), r); + // A packed value is at {slot, byte offset}. + Value slot = genPairField(addr, 0, loc); + Value offset = genPairField(addr, 1, loc); + + // An external function is cleaned by its packing, as upstream does. + Value preparedVal = isa(eltTy) + ? val + : evmB.genCleanup(eltTy, val, loc); + unsigned numBits; + if (sol::isBytesLikeType(eltTy)) { + // From MSB-aligned to LSB-aligned. + numBits = sol::getNumBytes(eltTy) * 8; + preparedVal = b.create(loc, preparedVal, + bExt.genI256Const(256 - numBits)); + } else if (sol::isAddressLikeType(eltTy)) { + numBits = 160; + } else if (auto intTy = dyn_cast(eltTy)) { + // A packed bool occupies a byte. + numBits = intTy.getWidth() == 1 ? 8 : intTy.getWidth(); + + // Signed integer cleanup sign-extends to i256, and the slot holds only + // the field slice. + if (intTy.isSigned() && numBits < 256) + preparedVal = b.create( + loc, preparedVal, + bExt.genI256Const(APInt::getLowBitsSet(256, numBits))); + } else if (isa(eltTy)) { + numBits = 64; + } else if (isa(eltTy)) { + // bytes24 + numBits = 192; + preparedVal = evmB.genExtFuncPack(preparedVal, /*inStorage=*/true, loc); + } else if (isa(eltTy)) { + // An enum has at most 256 members. + numBits = 8; } else { - llvm_unreachable("unexpected Sol loop op"); + llvm_unreachable("NYI"); } - r.eraseOp(rawOp); - return success(); - } -}; - -struct CallOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::CallOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - SmallVector convertedResTys; - if (failed(getTypeConverter()->convertTypes(op.getResultTypes(), - convertedResTys))) - return failure(); - r.replaceOpWithNewOp( - op, convertedResTys, op.getCalleeAttr(), adaptor.getOperands()); - return success(); + Value shiftBits = b.create(loc, offset, bExt.genI256Const(8)); + Value slotWithHole = evmB.genPunchHole(slot, shiftBits, numBits, dataLoc); + preparedVal = b.create(loc, preparedVal, shiftBits); + evmB.genStore(b.create(loc, slotWithHole, preparedVal), slot, + dataLoc); + return; } -}; - -struct ReturnOpLowering - : public CleanedOperandsLowering { - using Base = CleanedOperandsLowering; - using Base::Base; - - LogicalResult rewriteCleaned(sol::ReturnOp op, ConversionPatternRewriter &r, - ArrayRef operands) const { - r.replaceOpWithNewOp(op, operands); - return success(); + default: + llvm_unreachable("NYI: Calldata data-location"); } -}; +} -struct FuncOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; +Value Lowering::lower(sol::DataLocCastOp op) { + Type srcTy = op.getInp().getType(); + assert(!isa(srcTy)); + assert(sol::getDataLocation(op.getType()) == sol::DataLocation::Memory && + sol::isNonPtrRefType(srcTy) && "unexpected data location cast"); - LogicalResult matchAndRewrite(sol::FuncOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - // Set llvm.linkage attribute to private if not explicitly specified. - std::vector attrs; - if (auto linkageAttr = op->getAttr("llvm.linkage")) - attrs.push_back(r.getNamedAttr("llvm.linkage", linkageAttr)); - else - attrs.push_back(r.getNamedAttr( - "llvm.linkage", - LLVM::LinkageAttr::get(r.getContext(), LLVM::Linkage::Private))); - - auto convertedFuncTy = cast( - getTypeConverter()->convertType(op.getFunctionType())); - // FIXME: The location of the block arguments are lost here! - auto newOp = - r.create(op.getLoc(), op.getName(), convertedFuncTy); - for (NamedAttribute attr : attrs) - newOp->setAttr(attr.getName(), attr.getValue()); - r.inlineRegionBefore(op.getBody(), newOp.getBody(), newOp.getBody().end()); - r.eraseOp(op); - return success(); - } -}; + Location loc = op.getLoc(); + evm::Builder evmB = evmBuilder(loc); + sol::DataLocation srcDataLoc = sol::getDataLocation(srcTy); + Value src = lowered(op.getInp()); -struct ICallOpLowering : public OpConversionPattern { - ICallOpLowering(TypeConverter &tyConv, MLIRContext *ctx, - evm::InternalDispatch &dispatch) - : OpConversionPattern(tyConv, ctx), dispatch(dispatch) {} - - LogicalResult matchAndRewrite(sol::ICallOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - auto calleeArgs = adaptor.getOperands().drop_front(); - SmallVector convertedResTys; - if (failed(getTypeConverter()->convertTypes(op.getResultTypes(), - convertedResTys))) - return failure(); - - auto calleeTy = mlir::FunctionType::get( - r.getContext(), calleeArgs.getTypes(), convertedResTys); - SmallVector dispatchInputTys{adaptor.getCallee().getType()}; - llvm::append_range(dispatchInputTys, calleeTy.getInputs()); - auto dispatchFnTy = mlir::FunctionType::get( - r.getContext(), dispatchInputTys, calleeTy.getResults()); - - SmallVector callArgs{adaptor.getCallee()}; - llvm::append_range(callArgs, calleeArgs); - r.replaceOpWithNewOp( - op, convertedResTys, - FlatSymbolRefAttr::get( - dispatch.getOrCreateDispatchFn(op, dispatchFnTy, r)), - callArgs); - return success(); - } + // A calldata struct, or a calldata array of dynamically encoded elements, + // is copied to memory by decoding it against the end of calldata, which + // validates every nested offset and length. + // old codegen begin + // Every other calldata array and bytes stays a raw copy with no checks. + // (Via-IR decodes every calldata array copy.) + auto srcArrTy = dyn_cast(srcTy); + bool decodesFromCallData = + isa(srcTy) || + (srcArrTy && sol::hasDynamicallySizedElt(srcArrTy.getEltType())); + // old codegen end + if (srcDataLoc != sol::DataLocation::CallData || !decodesFromCallData) + return evmB.genCloneToMemory(srcTy, src, srcDataLoc); + + // The decoder reads a dynamic array from its length word, which precedes + // the data of every calldata array: a slice cannot have a dynamically + // encoded element type. + Value addr = src; + if (srcArrTy && srcArrTy.isDynSized()) + addr = b.create(loc, evmB.genDataAddrPtr(src, srcTy), + solgen::BuilderExt(b, loc).genI256Const(32)); + return evmB.genABITupleDecoding( + op.getType(), addr, /*fromMem=*/false, + /*tupleEnd=*/b.create(loc), /*topLevel=*/false, loc); +} - evm::InternalDispatch &dispatch; -}; +Value Lowering::lower(sol::LengthOp op) { + Type ty = op.getInp().getType(); + auto arrTy = dyn_cast(ty); + assert((isa(ty) || arrTy) && "NYI"); + if (arrTy && !arrTy.isDynSized()) + return solgen::BuilderExt(b, op.getLoc()).genI256Const(arrTy.getSize()); + return evmBuilder(op.getLoc()).genDynSize(lowered(op.getInp()), ty); +} -struct ContractOpLowering : public OpRewritePattern { - const char *libAddrName = "library_deploy_address"; +Value Lowering::lower(sol::SliceOp op) { + Location loc = op.getLoc(); + solgen::BuilderExt bExt(b, loc); + evm::Builder evmB = evmBuilder(loc); + + Type arrTy = op.getArr().getType(); + assert(sol::getDataLocation(arrTy) == sol::DataLocation::CallData); + + Value arr = lowered(op.getArr()); + Value start = + evmB.genCleanup(op.getStart().getType(), lowered(op.getStart()), loc); + Value end = evmB.genCleanup(op.getEnd().getType(), lowered(op.getEnd()), loc); + + Value dataAddr = evmB.genDataAddrPtr(arr, arrTy); + Value length = evmB.genDynSize(arr, arrTy); + + auto startGtEnd = bExt.genCmp(yul::CmpPredicate::ugt, start, end); + evmB.genDebugRevertWithMsg(startGtEnd, "Slice starts after end", loc); + auto endGtLen = bExt.genCmp(yul::CmpPredicate::ugt, end, length); + evmB.genDebugRevertWithMsg(endGtLen, "Slice is greater than length", loc); + + unsigned stride = 32; + if (auto arrType = dyn_cast(arrTy)) + stride = evm::getCallDataHeadSize(arrType.getEltType()); + else if (isa(arrTy)) + stride = 1; + + Value scaledStart = + b.create(loc, start, bExt.genI256Const(stride)); + Value newOffset = b.create(loc, dataAddr, scaledStart); + Value newLength = b.create(loc, end, start); + return bExt.genLLVMStruct({newOffset, newLength}); +} - ContractOpLowering(MLIRContext *ctx, const TypeConverter &tyConv) - : OpRewritePattern(ctx), tyConv(tyConv) {} +void Lowering::lower(sol::CopyOp op) { + Location loc = op.getLoc(); + evm::Builder evmB = evmBuilder(loc); - /// The pass's type converter, which the decoders of the dispatcher and the - /// constructor give their results' types with. - const TypeConverter &tyConv; + if (isa(op.getSrc().getType())) { + StringRef bytes = + cast(op.getSrc().getDefiningOp()).getValue(); + evmB.genCopyConstantStringToStorage(bytes, lowered(op.getDst()), loc); + return; + } - /// Generate the call value check. - void genCallValChk(ModuleOp mod, PatternRewriter &r, Location loc) const { - mlir::solgen::BuilderExt bExt(r, loc); - evm::Builder evmB(mod, r, loc); + Type srcTy = op.getSrc().getType(); + Type dstTy = op.getDst().getType(); + evmB.genCopy(srcTy, dstTy, lowered(op.getSrc()), lowered(op.getDst()), + sol::getDataLocation(srcTy), sol::getDataLocation(dstTy), loc); +} - auto callVal = r.create(loc); - auto callValChk = - bExt.genCmp(yul::CmpPredicate::ne, callVal, bExt.genI256Const(0)); - evmB.genDebugRevertWithMsg(callValChk, "Ether sent to non-payable function", - loc); - }; +void Lowering::lower(sol::DeleteOp op) { + Type refTy = op.getReference().getType(); + assert(sol::getDataLocation(refTy) == sol::DataLocation::Storage && + "sol.delete expects a Storage reference"); + evmBuilder(op.getLoc()) + .genClearStorageValue(refTy, lowered(op.getReference()), op.getLoc()); +} - /// Generate the free pointer initialization. A memoryguard lets the backend - /// claim the memory below it for spills, which memory-unsafe inline assembly - /// rules out. - void genFreePtrInit(sol::ContractOp contrOp, PatternRewriter &r, Location loc, - size_t reservedMem = 0) const { - mlir::solgen::BuilderExt bExt(r, loc); - int64_t start = - mlir::evm::MemoryLayout::generalPurposeMemoryStart + reservedMem; - Value freePtr; - if (contrOp.getUnsafeAsm()) - freePtr = bExt.genI256Const(start); - else - freePtr = r.create( - loc, r.getIntegerAttr(r.getIntegerType(256), start)); - r.create(loc, bExt.genI256Const(64), freePtr); - }; +//===----------------------------------------------------------------------===// +// ABI +//===----------------------------------------------------------------------===// - /// Generates the dispatch to interface functions. - void genDispatch(sol::ContractOp contrOp, - SmallVector const &selectors, - SmallVector &ifcFns, sol::FuncOp fallbackFn, - PatternRewriter &r, Location loc) const { - mlir::solgen::BuilderExt bExt(r, loc); - evm::Builder evmB(getModule(contrOp), r, loc); - - Value notDelegateCallCond; - if (contrOp.getKind() == sol::ContractKind::Library) { - auto libAddr = r.create(loc, r.getIntegerType(256), - libAddrName); - auto currAddr = r.create(loc); - notDelegateCallCond = - bExt.genCmp(yul::CmpPredicate::eq, libAddr, currAddr); - } +/// Returns the types the ABI encodes `tys` as, given the callee's `boundaryTys` +/// and whether it `isLibrary`: a library takes a storage reference as its +/// slot. +static SmallVector getABITypes(OpBuilder &b, TypeRange tys, + TypeRange boundaryTys, bool isLibrary) { + if (!isLibrary) + return SmallVector(tys); + return llvm::map_to_vector( + llvm::zip_equal(tys, boundaryTys), [&](auto tyPair) -> Type { + auto [ty, boundaryTy] = tyPair; + if (sol::getDataLocation(boundaryTy) == sol::DataLocation::Storage) + return b.getIntegerType(256); + return ty; + }); +} - // Do nothing if there are no interface functions. - if (ifcFns.empty()) - return; +Value Lowering::lower(sol::EncodeOp op) { + Location loc = op.getLoc(); + solgen::BuilderExt bExt(b, loc); + evm::Builder evmB = evmBuilder(loc); + + // An encoding that only the next op hashes is read before anything else + // allocates, so it takes no memory of its own. + Operation *user = op->hasOneUse() ? *op->user_begin() : nullptr; + bool reserve = + !isa_and_nonnull(user) || user != op->getNextNode(); + + SmallVector ins = lowered(op.getIns()); + Value freePtr = evmB.genFreePtr(); + Value dataStart, dataEnd; + if (op.getPacked()) { + dataStart = b.create(loc, freePtr, bExt.genI256Const(32)); + dataEnd = evmB.genABIEncodingPacked(op.getIns().getType(), ins, dataStart); + } else if (op.getSelector()) { + dataStart = b.create(loc, freePtr, bExt.genI256Const(32)); + b.create(loc, dataStart, lowered(op.getSelector())); + Value tupleStart = + b.create(loc, dataStart, bExt.genI256Const(4)); + dataEnd = evmB.genABIEncoding(op.getIns().getType(), ins, tupleStart); + } else { + dataStart = b.create(loc, freePtr, bExt.genI256Const(32)); + dataEnd = evmB.genABIEncoding(op.getIns().getType(), ins, dataStart); + } + Value dataSize = b.create(loc, dataEnd, dataStart); + b.create(loc, freePtr, dataSize); + if (reserve) + evmB.genFreePtrStore(dataEnd); + return freePtr; +} - // One call value test serves the whole switch when no route reachable - // from it accepts value: the cases are the interface functions, and the - // default falls through to the fallback. A receive function is only - // reached with empty calldata, outside the `calldatasize >= 4` block, so - // it does not count. A library emits no test, its functions cannot be - // payable. - auto isPayable = [](sol::FuncOp fn) { - return fn.getStateMutability() == sol::StateMutability::Payable; - }; - bool hoistCallValChk = contrOp.getKind() != sol::ContractKind::Library && - llvm::none_of(ifcFns, isPayable) && - (!fallbackFn || !isPayable(fallbackFn)); - - // Generate `if iszero(lt(calldatasize(), 4))` and set the insertion point - // to its then block. - auto callDataSz = r.create(loc); - auto callDataSzCmp = - bExt.genCmp(yul::CmpPredicate::uge, callDataSz, bExt.genI256Const(4)); - auto ifOp = bExt.createIf(callDataSzCmp); - OpBuilder::InsertionGuard insertGuard(r); - r.setInsertionPointToStart(&ifOp.getThenRegion().front()); - - if (hoistCallValChk) - genCallValChk(getModule(contrOp), r, loc); - - // Load the selector from the calldata. - auto callDataLd = r.create(loc, bExt.genI256Const(0)); - Value callDataSelector = - r.create(loc, callDataLd, bExt.genI256Const(224)); - - SmallVector selectorValues; - selectorValues.reserve(selectors.size()); - for (uint32_t selector : selectors) - selectorValues.emplace_back(/*numBits=*/256, selector); - auto selectorsAttr = mlir::DenseIntElementsAttr::get( - mlir::RankedTensorType::get(static_cast(selectors.size()), - r.getIntegerType(256)), - selectorValues); - - // Generate the switch op. - auto switchOp = r.create(loc, /*resultTypes=*/TypeRange{}, - callDataSelector, selectorsAttr, - selectors.size()); - - // Generate the default block. - { - OpBuilder::InsertionGuard insertGuard(r); - r.setInsertionPointToStart(r.createBlock(&switchOp.getDefaultRegion())); - r.create(loc); - } +SmallVector Lowering::lower(sol::DecodeOp op) { + Location loc = op.getLoc(); + evm::Builder evmB = evmBuilder(loc); + + Type srcTy = op.getAddr().getType(); + auto srcStringTy = dyn_cast(srcTy); + assert(srcStringTy && + "abi.decode source must be bytes memory/calldata in sol dialect"); + sol::DataLocation dataLoc = srcStringTy.getDataLocation(); + assert((dataLoc == sol::DataLocation::Memory || + dataLoc == sol::DataLocation::CallData) && + "abi.decode expects memory/calldata byte buffer"); + + Value addr = lowered(op.getAddr()); + Value tupleStart = evmB.genDataAddrPtr(addr, srcTy); + Value tupleSize = evmB.genDynSize(addr, srcTy); + Value tupleEnd = b.create(loc, tupleStart, tupleSize); + std::vector results; + evmB.genABITupleDecoding(op.getResultTypes(), tupleStart, tupleEnd, results, + /*fromMem=*/dataLoc == sol::DataLocation::Memory); + return SmallVector(results.begin(), results.end()); +} - for (auto [caseRegion, ifcFnOp] : - llvm::zip(switchOp.getCaseRegions(), ifcFns)) { - OpBuilder::InsertionGuard insertGuard(r); - mlir::Block *caseBlk = r.createBlock(&caseRegion); - r.setInsertionPointToStart(caseBlk); - - // A case runs its interface function, so it is at that function's - // location. - Location fnLoc = evm::sourceLoc(ifcFnOp.getLoc()); - mlir::solgen::BuilderExt fnBExt(r, fnLoc); - evm::Builder fnEvmB(getModule(contrOp), r, fnLoc); - - assert(ifcFnOp.getStateMutability()); - sol::StateMutability stateMutability = *ifcFnOp.getStateMutability(); - if (contrOp.getKind() == sol::ContractKind::Library) { - assert(stateMutability != sol::StateMutability::Payable); - if (stateMutability > sol::StateMutability::View) { - assert(notDelegateCallCond); - fnEvmB.genDebugRevertWithMsg( - notDelegateCallCond, - "Non-view function of library called without DELEGATECALL", - fnLoc); - } - } +void Lowering::lower(sol::EmitOp op) { + Location loc = op.getLoc(); + solgen::BuilderExt bExt(b, loc); + evm::Builder evmB = evmBuilder(loc); - if (!hoistCallValChk && contrOp.getKind() != sol::ContractKind::Library && - stateMutability != sol::StateMutability::Payable) { - genCallValChk(getModule(contrOp), r, fnLoc); - } + std::vector indexedArgs; + if (op.getSignature()) + indexedArgs.push_back( + bExt.genI256Const(evm::keccak256AsAPInt(op.getSignature()->str()))); + for (Value arg : op.getIndexedArgs()) { + Type ty = arg.getType(); - // Decode the input parameters (if required). - FunctionType origIfcFnTy = *ifcFnOp.getOrigFnType(); - SmallVector abiInputTys = - getABITypes(r, origIfcFnTy.getInputs(), origIfcFnTy.getInputs(), - contrOp.getKind() == sol::ContractKind::Library); - SmallVector abiResultTys = - getABITypes(r, origIfcFnTy.getResults(), origIfcFnTy.getResults(), - contrOp.getKind() == sol::ContractKind::Library); - std::vector decodedArgs; - if (!abiInputTys.empty()) - fnEvmB.genABITupleDecoding(abiInputTys, - /*tupleStart=*/fnBExt.genI256Const(4), - /*tupleEnd=*/callDataSz, decodedArgs, - /*fromMem=*/false, tyConv); - - // Generate the actual call. - auto callOp = r.create(fnLoc, ifcFnOp, decodedArgs); - - // Encode the result using the ABI's tuple encoder. - auto tupleStart = fnEvmB.genFreePtr(); - mlir::Value tupleSize; - if (!callOp.getResultTypes().empty()) { - auto tupleEnd = fnEvmB.genABIEncoding(abiResultTys, callOp.getResults(), - tupleStart); - tupleSize = r.create(fnLoc, tupleEnd, tupleStart); - } else { - tupleSize = fnBExt.genI256Const(0); - } + if (isa(ty)) { + StringRef bytes = + cast(arg.getDefiningOp()).getValue(); + indexedArgs.push_back(bExt.genI256Const(evm::keccak256AsAPInt(bytes))); + continue; + } - // Generate the return. - assert(tupleSize); - r.create(fnLoc, tupleStart, tupleSize); + if (isa(ty)) { + Value scratchStart = evmB.genFreePtr(); + Value scratchEnd = evmB.genABIEncodingPacked( + TypeRange{ty}, ValueRange{lowered(arg)}, scratchStart, loc); + Value scratchSize = b.create(loc, scratchEnd, scratchStart); + indexedArgs.push_back( + b.create(loc, scratchStart, scratchSize)); + continue; } - } - /// The functions a contract's code calls: the constructor, the interface - /// functions with their selectors, and receive and fallback. - struct ContractFns { - sol::FuncOp ctor, receive, fallback; - SmallVector ifc; - SmallVector selectors; - }; + indexedArgs.push_back( + isa(ty) + ? evmB.genExtFuncPack(lowered(arg), /*inStorage=*/false, loc) + : evmB.genCleanup(ty, lowered(arg), loc)); + } - /// Generates the creation code: it runs the constructor `ctor`, if any, then - /// returns the runtime object's code with the immutables set. - void genCreationCode(sol::ContractOp contrOp, sol::FuncOp ctor, - StringRef runtimeObjName, PatternRewriter &r, - Location loc) const { - mlir::solgen::BuilderExt bExt(r, loc); - ModuleOp mod = getModule(contrOp); + Value tupleStart = evmB.genFreePtr(); + Value tupleEnd = + evmB.genABIEncoding(op.getNonIndexedArgs().getTypes(), + lowered(op.getNonIndexedArgs()), tupleStart); + Value tupleSize = b.create(loc, tupleEnd, tupleStart); + b.create(loc, tupleStart, tupleSize, indexedArgs); +} - SmallVector immOps( - contrOp.getBody()->getOps()); +void Lowering::lower(sol::RevertOp op) { + evm::Builder evmB = evmBuilder(op.getLoc()); + if (op.getCall()) { + assert(op.getSignature() && !op.getSignature()->empty()); + evmB.genRevert(op.getArgs().getTypes(), lowered(op.getArgs()), + *op.getSignature()); + } else if (op.getSignature()) { + // An empty reason still ABI-encodes Error(""). + evmB.genUserRevertWithMsg(op.getSignature()->str()); + } else { + evmB.genRevert(op.getLoc()); + } +} - genFreePtrInit(contrOp, r, loc, - immOps.size() * evm::MemoryLayout::immutableCellSize); +//===----------------------------------------------------------------------===// +// External calls +//===----------------------------------------------------------------------===// - // The constructor's check, argument decoding and call run the constructor, - // so they are at its location. - Location ctorLoc = ctor ? evm::sourceLoc(ctor.getLoc()) : loc; - evm::Builder ctorEvmB(mod, r, ctorLoc); +SmallVector Lowering::genExtCall(const ExtCall &call, Location loc) { + solgen::BuilderExt bExt(b, loc); + evm::Builder evmB = evmBuilder(loc); - if (!ctor) { - genCallValChk(mod, r, loc); - } else { - assert(ctor.getStateMutability()); - if (*ctor.getStateMutability() != sol::StateMutability::Payable) - genCallValChk(mod, r, ctorLoc); - } + assert(sol::evmCanOverchargeGasForCall(mod) && "NYI"); + // TODO: The return info (solidity::frontend::ReturnInfo) assumes an EVM + // with return data, copied with returndatacopy and returndatasize. + assert(sol::evmSupportsReturnData(mod) && "NYI"); - // Generate the call to constructor (if required). - if (ctor && contrOp.getKind() != sol::ContractKind::Library) { - std::vector decodedArgs; - FunctionType ctorFnTy = *ctor.getOrigFnType(); - if (!ctorFnTy.getInputs().empty()) { - auto progSize = r.create(ctorLoc, contrOp.getName()); - auto codeSize = r.create(ctorLoc); - auto argSize = r.create(ctorLoc, codeSize, progSize); - Value tupleStart = ctorEvmB.genMemAlloc(argSize); - r.create(ctorLoc, tupleStart, progSize, argSize); - ctorEvmB.genABITupleDecoding( - ctorFnTy.getInputs(), tupleStart, - /*tupleEnd=*/r.create(ctorLoc, tupleStart, argSize), - decodedArgs, - /*fromMem=*/true, tyConv); - } - r.create(ctorLoc, ctor, decodedArgs); - } + SmallVector abiInputTys = getABITypes( + b, call.ins.getTypes(), call.calleeInputTys, call.libraryCall); + // The results decode into the types the caller holds: a calldata-typed + // return lands in memory. + SmallVector abiResultTys = + getABITypes(b, call.resultTys, call.resultTys, call.libraryCall); + + unsigned totHeadSize = 0; + for (Type resTy : abiResultTys) + totHeadSize += evm::getCallDataHeadSize(resTy); + bool extCodeSizeCheck = totHeadSize == 0 || + !sol::evmSupportsReturnData(mod) || + sol::shouldEmitDebugRevertStrings(mod); + + Value addr = call.addr; + // old codegen begin + // (Via-IR cleans on casts, not here). + if (isa(call.addrTy)) + addr = evmB.genCleanup(call.addrTy, addr, loc); + // old codegen end - // Generate the codecopy of the runtime object for the return from the - // creation object. - auto freePtr = r.create(loc, bExt.genI256Const(64)); - auto runtimeObjSym = FlatSymbolRefAttr::get(r.getContext(), runtimeObjName); - auto runtimeObjOffset = r.create(loc, runtimeObjSym); - auto runtimeObjSize = r.create(loc, runtimeObjSym); - r.create(loc, freePtr, runtimeObjOffset, runtimeObjSize); - - // Generate setimmutable of the library address (the runtime object uses - // this for the delegate call check). - if (contrOp.getKind() == sol::ContractKind::Library) - r.create(loc, freePtr, libAddrName, - r.create(loc)); - - // Generate setimmutable's. - for (sol::ImmutableOp immOp : immOps) { - auto addr = bExt.genI256Const(evm::getImmutableAddr(immOp)); - auto val = r.create(loc, addr); - r.create(loc, freePtr, immOp.getName(), val); + if (extCodeSizeCheck) { + auto extCodeSize = b.create(loc, addr); + auto isExtCodeSizeZero = + bExt.genCmp(yul::CmpPredicate::eq, extCodeSize, bExt.genI256Const(0)); + evmB.genDebugRevertWithMsg(isExtCodeSizeZero, + "Target contract does not contain code"); + } + + Value selectorAddr = evmB.genFreePtr(); + Value shiftedSelector = + b.create(loc, call.selector, bExt.genI256Const(224)); + b.create(loc, selectorAddr, shiftedSelector); + + Value tupleStart = + b.create(loc, selectorAddr, bExt.genI256Const(4)); + Value tupleEnd = + evmB.genABIEncoding(abiInputTys, lowered(call.ins), tupleStart); + + // The static return size, unless it is dynamic (from + // solidity::frontend::ReturnInfo). A struct or array with a dynamically + // sized element encodes as a tail whose size is not static. + unsigned staticRetSizeVal = 0; + bool isRetSizeDynamic = false; + for (Type ty : abiResultTys) { + if (sol::hasDynamicallySizedElt(ty)) { + isRetSizeDynamic = true; + staticRetSizeVal = 0; + break; } - - // Generate the return for the creation context. - r.create(loc, freePtr, runtimeObjSize); + staticRetSizeVal += evm::getCallDataHeadSize(ty); } - /// Generates the runtime code: the dispatch to the interface functions, then - /// receive and fallback. - void genRuntimeCode(sol::ContractOp contrOp, ContractFns &fns, - PatternRewriter &r, Location loc) const { - mlir::solgen::BuilderExt bExt(r, loc); - ModuleOp mod = getModule(contrOp); - evm::Builder evmB(mod, r, loc); - - // Generate the memory init. - genFreePtrInit(contrOp, r, loc); - - // Generate the dispatch to interface functions. - genDispatch(contrOp, fns.selectors, fns.ifc, fns.fallback, r, loc); - - // Generate receive function. - if (fns.receive) { - auto callDataSz = r.create(loc); - auto callDataSzIsZero = - bExt.genCmp(yul::CmpPredicate::eq, callDataSz, bExt.genI256Const(0)); - OpBuilder::InsertionGuard insertGuard(r); - auto ifOp = r.create(loc, TypeRange{}, callDataSzIsZero); - r.setInsertionPointToStart(&ifOp.getThenRegion().emplaceBlock()); - r.create(loc, fns.receive, /*operands=*/ValueRange{}); - r.create(loc); - } + Value inpSize = b.create(loc, tupleEnd, selectorAddr); + Value staticRetSize = bExt.genI256Const(staticRetSizeVal); + // A call that names no gas forwards all the gas left, read here so that it + // is not kept live across the encoding. + Value gas = call.gas ? call.gas : b.create(loc); + Value status; + // A delegate call may also be static, and is checked first. + if (call.delegateCall) + status = b.create(loc, gas, addr, + /*inpOffset=*/selectorAddr, inpSize, + /*outOffset=*/selectorAddr, + /*outSize=*/staticRetSize); + else if (call.staticCall) + status = b.create(loc, gas, addr, + /*inpOffset=*/selectorAddr, inpSize, + /*outOffset=*/selectorAddr, + /*outSize=*/staticRetSize); + else + status = b.create(loc, gas, addr, call.val, + /*inpOffset=*/selectorAddr, inpSize, + /*outOffset=*/selectorAddr, + /*outSize=*/staticRetSize); + + // A try call leaves a failure to the try's clauses. + if (!call.tryCall) { + auto statusIsZero = + bExt.genCmp(yul::CmpPredicate::eq, status, bExt.genI256Const(0)); + evmB.genForwardingRevert(statusIsZero); + } - // Generate fallback function. - if (fns.fallback) { - FunctionType fallbackFnTy = fns.fallback.getFunctionType(); - // Both valid fallback forms (with and without bytes) match counts. - assert(fallbackFnTy.getNumInputs() == fallbackFnTy.getNumResults() && - "fallback has mismatched parameter/return counts"); + SmallVector decodedResultTys = + llvm::map_to_vector(call.resultTys, evm::getYulType); - if (fns.fallback.getStateMutability() != sol::StateMutability::Payable) { - genCallValChk(mod, r, loc); - } + // The results are decoded on success. A failure yields undefs that + // execution never reads: it either skips the try's success clause or has + // reverted. + auto statusIsNotZero = + bExt.genCmp(yul::CmpPredicate::ne, status, bExt.genI256Const(0)); + auto statusIfOp = b.create(loc, /*resultTys=*/decodedResultTys, + /*cond=*/statusIsNotZero); + OpBuilder::InsertionGuard guard(b); + b.setInsertionPointToStart(&statusIfOp.getElseRegion().emplaceBlock()); + SmallVector undefYields; + for (Type ty : decodedResultTys) + undefYields.push_back(b.create(loc, ty)); + b.create(loc, undefYields); + + // The return data reuses the memory at `selectorAddr`. + b.setInsertionPointToStart(&statusIfOp.getThenRegion().emplaceBlock()); + std::vector decodedResults; + if (isRetSizeDynamic) { + Value retDataSize = b.create(loc); + b.create(loc, selectorAddr, + /*src=*/bExt.genI256Const(0), retDataSize); + evmB.genFreePtrUpd(selectorAddr, retDataSize); + Value tupleEnd = b.create(loc, selectorAddr, retDataSize); + evmB.genABITupleDecoding(abiResultTys, selectorAddr, tupleEnd, + decodedResults, /*fromMem=*/true); + } else { + // See https://github.com/ethereum/solidity/pull/12684: the tuple is the + // return data up to the static size, so return data shorter than that + // fails the tuple's size check, and the tuple is otherwise exactly the + // static size. + if (staticRetSizeVal) { + Value retDataSize = b.create(loc); + Value tooShort = + bExt.genCmp(yul::CmpPredicate::ult, retDataSize, staticRetSize); + evmB.genDebugRevertWithMsg(tooShort, + "ABI decoding: tuple data too short"); + } + // Scalar results are decoded onto the stack right away, so the return + // data needs no memory of its own. + if (!llvm::all_of(abiResultTys, [](Type ty) { return sol::isScalar(ty); })) + evmB.genFreePtrUpd(selectorAddr, staticRetSize); + Value tupleEnd = b.create(loc, selectorAddr, staticRetSize); + evmB.genABITupleDecoding(abiResultTys, selectorAddr, tupleEnd, + decodedResults, /*fromMem=*/true); + } + b.create(loc, decodedResults); + + SmallVector results{statusIsNotZero}; + llvm::append_range(results, statusIfOp.getResults()); + return results; +} - // The `bytes calldata` parameter, when present, is the full call payload: - // a `{offset, length}` reference of `{0, calldatasize()}`. - SmallVector fallbackArgs; - if (fallbackFnTy.getNumInputs() == 1) { - Value callDataSz = r.create(loc); - fallbackArgs.push_back( - bExt.genLLVMStruct({bExt.genI256Const(0), callDataSz}, loc)); - } +SmallVector Lowering::lower(sol::ExtCallOp op) { + ExtCall call{op.getIns(), + op.getCalleeType().getInputs(), + TypeRange(op.getResultTypes()).drop_front(), + lowered(op.getAddr()), + op.getAddr().getType(), + lowered(op.getSelector()), + op.getGas() ? lowered(op.getGas()) : Value{}, + op.getVal() ? lowered(op.getVal()) : Value{}, + op.getTryCall(), + op.getStaticCall(), + op.getDelegateCall(), + op.getLibraryCall()}; + return genExtCall(call, op.getLoc()); +} - auto call = r.create(loc, fns.fallback, fallbackArgs); +SmallVector Lowering::lower(sol::ExtICallOp op) { + Location loc = op.getLoc(); + Value callee = lowered(op.getCallee()); + Value addr = genPairField(callee, 0, loc); + Value selector = genPairField(callee, 1, loc); + FunctionType calleeTy = + cast(op.getCallee().getType()).getFuncTy(); + ExtCall call{op.getCalleeOperands(), + calleeTy.getInputs(), + TypeRange(op.getResultTypes()).drop_front(), + addr, + addr.getType(), + selector, + op.getGas() ? lowered(op.getGas()) : Value{}, + op.getValue() ? lowered(op.getValue()) : Value{}, + op.getTryCall(), + op.getStaticCall(), + /*delegateCall=*/false, + /*libraryCall=*/false}; + return genExtCall(call, loc); +} - if (fallbackFnTy.getNumResults() == 1) { - // The returned `bytes memory` is returned verbatim (not ABI-encoded), - // i.e. `return(add(ret, 0x20), mload(ret))`. - Value ret = call.getResult(0); - Value dataPtr = - evmB.genDataAddrPtr(ret, sol::DataLocation::Memory, loc); - Value len = r.create(loc, ret); - r.create(loc, dataPtr, len); - } else { - r.create(loc); - } +template +SmallVector Lowering::lowerBareCall(OpT op) { + Location loc = op.getLoc(); + solgen::BuilderExt bExt(b, loc); + evm::Builder evmB = evmBuilder(loc); + assert(sol::evmSupportsReturnData(mod) && "NYI"); - } else { - evmB.genDebugRevertWithMsg( - fns.receive ? "Unknown signature and no fallback defined" - : "Contract does not have fallback nor receive functions", - loc); - } + Value inpSize, inpStart; + if (isa(op.getInp().getType())) { + StringRef bytes = + cast(op.getInp().getDefiningOp()).getValue(); + // The callee reads the input only during the call, so it skips the + // allocation a memory literal gets: it is written at the free memory + // pointer, and the pointer does not move. + inpStart = evmB.genFreePtr(loc); + evmB.genStringLiteralData(bytes, inpStart, loc); + inpSize = bExt.genI256Const(bytes.size()); + } else { + auto inpTy = cast(op.getInp().getType()); + inpSize = evmB.genDynSize(lowered(op.getInp()), inpTy, loc); + inpStart = evmB.genDataAddrPtr(lowered(op.getInp()), inpTy, loc); } + // old codegen begin + // (Via-IR cleans on casts, not here). + Value addr = + evmB.genCleanup(op.getAddr().getType(), lowered(op.getAddr()), loc); + // old codegen end + Value zero = bExt.genI256Const(0); + // A call that names no gas forwards all the gas left, read here so that it + // is not kept live across the input size and address computation. + Value gas = op.getGas() ? lowered(op.getGas()) : b.create(loc); + Value rawStatus; + if constexpr (std::is_same_v) + rawStatus = b.create(loc, gas, addr, lowered(op.getVal()), + /*inpOffset=*/inpStart, inpSize, + /*outOffset=*/zero, /*outSize=*/zero); + else + rawStatus = b.create(loc, gas, addr, /*inpOffset=*/inpStart, + inpSize, /*outOffset=*/zero, + /*outSize=*/zero); - LogicalResult matchAndRewrite(sol::ContractOp op, - PatternRewriter &r) const override { - // The code this pattern emits is at the contract's location, except the - // dispatch cases and the constructor's part of the creation code, which are - // at their function's. - mlir::Location loc = op.getLoc(); - - std::string runtimeObjName = std::string(op.getName()) + "_deployed"; - auto obj = r.create( - loc, op.getRuntime() ? StringRef(runtimeObjName) : op.getName()); - - ContractFns fns; - for (auto fn : op.getBody()->getOps()) { - if (auto selector = fn.getSelector()) { - fns.selectors.push_back(*selector); - fns.ifc.push_back(fn); - } - if (fn.getKind() == sol::FunctionKind::Constructor) { - assert(!fns.ctor); - fns.ctor = fn; - } else if (fn.getKind() == sol::FunctionKind::Receive) { - assert(!fns.receive); - fns.receive = fn; - } else if (fn.getKind() == sol::FunctionKind::Fallback) { - assert(!fns.fallback); - fns.fallback = fn; - } - } + Value status = bExt.genCmp(yul::CmpPredicate::ne, rawStatus, zero); + // Return data nobody reads is not copied, whatever its size. + Value retData = op.getRetData().use_empty() + ? bExt.genI256Const(mlir::evm::MemoryLayout::zeroPointer) + : evmB.genReturnDataBytes(loc); + return {status, retData}; +} - ModuleOp mod = getModule(op); - StringRef entryName = yul::FuncOp::entryName; - r.setInsertionPointToStart(obj.getBody()); - auto entryFn = r.create( - evm::fuseArtificialSubprogram(mod, entryName, mod.getLoc()), entryName, - r.getFunctionType({}, {})); - r.createBlock(&entryFn.getBody()); - if (op.getRuntime()) - genRuntimeCode(op, fns, r, loc); - else - genCreationCode(op, fns.ctor, runtimeObjName, r, loc); - - // The functions and the string literal globals follow the entry function, - // the last one first, which keeps the layout of the object and so its - // bytecode. - for (Operation &i : - llvm::make_early_inc_range(llvm::reverse(*op.getBody()))) { - if (isa(i)) - r.moveOpBefore(&i, obj.getBody(), obj.getBody()->end()); - else if (!isa(i)) - llvm_unreachable("NYI"); - } +template +Value Lowering::genValueTransferStatus(OpT op, yul::CmpPredicate pred) { + Location loc = op.getLoc(); + solgen::BuilderExt bExt(b, loc); + evm::Builder evmB = evmBuilder(loc); + Value val = lowered(op.getVal()); + Value zero = bExt.genI256Const(0); + Value callStipend = bExt.genI256Const(2300); + Value valueIsZero = bExt.genCmp(yul::CmpPredicate::eq, val, zero); + Value gas = b.create(loc, valueIsZero, callStipend, zero); + // old codegen begin + // (Via-IR cleans on casts, not here). + Value addr = + evmB.genCleanup(op.getAddr().getType(), lowered(op.getAddr()), loc); + // old codegen end + Value callStatus = b.create(loc, gas, addr, val, + /*inpOffset=*/zero, /*inpSize=*/zero, + /*outOffset=*/zero, + /*outSize=*/zero); + return bExt.genCmp(pred, callStatus, zero); +} + +Value Lowering::lower(sol::SendOp op) { + return genValueTransferStatus(op, yul::CmpPredicate::ne); +} + +void Lowering::lower(sol::TransferOp op) { + Value statusIsZero = genValueTransferStatus(op, yul::CmpPredicate::eq); + evmBuilder(op.getLoc()).genForwardingRevert(statusIsZero); +} - r.eraseOp(op); - return success(); +Value Lowering::lower(sol::NewOp op) { + Location loc = op.getLoc(); + solgen::BuilderExt bExt(b, loc); + evm::Builder evmB = evmBuilder(loc); + + Value bytecodeAddr = evmB.genFreePtr(); + Value dataOffset = b.create(loc, op.getObjName()); + Value dataSize = b.create(loc, op.getObjName()); + b.create(loc, bytecodeAddr, dataOffset, dataSize); + + Value tupleStart = b.create(loc, bytecodeAddr, dataSize); + Value tupleEnd = evmB.genABIEncoding(op.getCtorArgs().getType(), + lowered(op.getCtorArgs()), tupleStart); + Value allocSize = b.create(loc, tupleEnd, bytecodeAddr); + + Value addr; + if (op.getSalt()) + addr = b.create(loc, lowered(op.getVal()), bytecodeAddr, + allocSize, lowered(op.getSalt())); + else + addr = b.create(loc, lowered(op.getVal()), bytecodeAddr, + allocSize); + + if (!op.getTryCall()) { + Value zero = bExt.genI256Const(0); + Value addrIsZero = bExt.genCmp(yul::CmpPredicate::eq, addr, zero); + evmB.genForwardingRevert(addrIsZero); } -}; + return addr; +} -} // namespace +Value Lowering::lower(sol::ObjectCodeOp op) { + Location loc = op.getLoc(); + evm::Builder evmB = evmBuilder(loc); + Value dataSize = b.create(loc, op.getObjName()); + Value alloc = evmB.genMemAlloc(op.getType(), /*zeroInit=*/false, + /*initVals=*/{}, dataSize, Type{}); + Value dataAddr = evmB.genDataAddrPtr(alloc, sol::DataLocation::Memory); + Value dataOffset = b.create(loc, op.getObjName()); + b.create(loc, dataAddr, dataOffset, dataSize); + return alloc; +} -// TODO: Assert that the type converter is compatible with the conversions. (We -// could only accept SolTypeConverter instead, but do we need need to be that -// strict?) (Also, can we do the assert at build time? If not, then only in -// debug builds?) - -void evm::populateArithPats(RewritePatternSet &pats, TypeConverter &tyConv) { - pats.add(pats.getContext()); - pats.add, - ArithBinOpLowering, - ArithBinOpLowering, - ArithBinOpLowering, - ArithBinOpLowering, - ArithBinOpLowering, NotOpLowering, - // Signed div/mod use the EVM sdiv/smod ops (defined wrapping on - // INT_MIN/-1) rather than the arith family, whose LLVM sdiv/srem are - // UB on that overflow. Unsigned has no such overflow, so the arith - // ops are fine there (div-by-zero is guarded by genPanic). - DivOrModOpLowering, - DivOrModOpLowering, - ExpOpLowering, ShrOpLowering, ShlOpLowering, CmpOpLowering, - AddModOpLowering, MulModOpLowering>(tyConv, pats.getContext()); +// old codegen begin +// (Via-IR cleans on casts, not here). +Value Lowering::lower(sol::CodeOp op) { + Location loc = op.getLoc(); + solgen::BuilderExt bExt(b, loc); + evm::Builder evmB = evmBuilder(loc); + Value addr = getCleaned(op)[0]; + + auto extCodeSize = b.create(loc, addr); + Value alloc = evmB.genFreePtr(); + b.create(loc, alloc, extCodeSize); + auto codeAddr = evmB.genDataAddrPtr(alloc, sol::DataLocation::Memory); + b.create(loc, addr, /*dstOffset=*/codeAddr, + /*srcOffset=*/bExt.genI256Const(0), extCodeSize); + evmB.genFreePtrStore(b.create(loc, codeAddr, extCodeSize)); + return alloc; } +// old codegen end -void evm::populateCheckedArithPats(RewritePatternSet &pats, - TypeConverter &tyConv) { - pats.add(tyConv, pats.getContext()); +Value Lowering::lower(sol::CodeSizeOp op) { + return b.create(op.getLoc(), getCleaned(op)[0]); } -void evm::populateCryptoPats(mlir::RewritePatternSet &pats, - mlir::TypeConverter &tyConv, - evm::StringLiteralPool &literals) { - pats.add( - tyConv, pats.getContext()); - pats.add(tyConv, pats.getContext(), - literals); +//===----------------------------------------------------------------------===// +// Try +//===----------------------------------------------------------------------===// + +// The status picks the success clause or the failure path. A failure whose +// return data starts with the selector of a caught Panic(uint256) or +// Error(string) runs that clause; any other failure runs the fallback clause, +// or reverts with the return data when there is none. The clauses are lowered +// once the code around them is. +void Lowering::lower(sol::TryOp op) { + Location loc = op.getLoc(); + solgen::BuilderExt bExt(b, loc); + evm::Builder evmB = evmBuilder(loc); + + SmallVector clauses; + + auto ifStatus = + b.create(loc, TypeRange{}, lowered(op.getStatus())); + assert(!op.getSuccessRegion().empty() && "a try has a success clause"); + clauses.push_back({&op.getSuccessRegion(), + &ifStatus.getThenRegion().emplaceBlock(), + &ifStatus.getThenRegion()}); + + Block *failure = &ifStatus.getElseRegion().emplaceBlock(); + b.setInsertionPointToStart(failure); + if (op.getFallbackRegion().empty()) { + evmB.genForwardingRevert(); + } else { + // The fallback clause's parameter is the return data as `bytes memory`. + Block &fallback = op.getFallbackRegion().front(); + if (fallback.getNumArguments() > 0) { + BlockArgument arg = fallback.getArgument(0); + mapLowered(arg, + evmBuilder(arg.getLoc()).genReturnDataBytes(arg.getLoc())); + } + } + + if (op.getPanicRegion().empty() && op.getErrorRegion().empty()) { + if (!op.getFallbackRegion().empty()) + clauses.push_back( + {&op.getFallbackRegion(), failure, &ifStatus.getElseRegion()}); + lowerClauses(clauses); + return; + } + + // The flag is cleared by a caught clause and keeps the fallback otherwise. + b.setInsertionPointToStart(failure); + auto runFallbackFlag = + b.create(loc, LLVM::LLVMPointerType::get(b.getContext()), + b.getIntegerType(256), bExt.genI256Const(1)); + auto genStoreFlag = [&](bool runFallback) { + Value flag = bExt.genBool(runFallback); + b.create(loc, flag, runFallbackFlag, + evm::getAlignment(runFallbackFlag)); + }; + genStoreFlag(true); + + // The return data holds a selector. + auto returnDataSize = b.create(loc); + auto selectorRetCond = + bExt.genCmp(yul::CmpPredicate::ugt, returnDataSize, bExt.genI256Const(3)); + auto ifSelectorRet = b.create(loc, TypeRange{}, selectorRetCond); + // What follows in the failure block is the fallback path. + Block::iterator fallbackPoint = b.getInsertionPoint(); + + b.setInsertionPointToStart(&ifSelectorRet.getThenRegion().emplaceBlock()); + b.setInsertionPoint(b.create(loc)); + auto zero = bExt.genI256Const(0); + b.create(loc, /*dst=*/zero, /*src=*/zero, + /*size=*/bExt.genI256Const(4)); + auto selectorWord = b.create(loc, zero); + auto selector = + b.create(loc, selectorWord, bExt.genI256Const(224)); + + SmallVector switchSelectors; + if (!op.getPanicRegion().empty()) + switchSelectors.emplace_back( + /*numBits=*/256, mlir::evm::selectorFromSignature("Panic(uint256)")); + if (!op.getErrorRegion().empty()) + switchSelectors.emplace_back( + /*numBits=*/256, mlir::evm::selectorFromSignature("Error(string)")); + auto switchSelectorsAttr = DenseIntElementsAttr::get( + RankedTensorType::get(static_cast(switchSelectors.size()), + b.getIntegerType(256)), + switchSelectors); + auto switchOp = b.create( + loc, TypeRange{}, selector, switchSelectorsAttr, switchSelectors.size()); + b.setInsertionPointToStart(&switchOp.getDefaultRegion().emplaceBlock()); + b.create(loc); + unsigned caseIdx = 0; + + if (!op.getPanicRegion().empty()) { + b.setInsertionPointToStart( + &switchOp.getCaseRegions()[caseIdx++].emplaceBlock()); + b.setInsertionPoint(b.create(loc)); + + // The return data holds the panic code. + auto panicRetCond = bExt.genCmp(yul::CmpPredicate::ugt, returnDataSize, + bExt.genI256Const(0x23)); + auto ifPanicRet = b.create(loc, TypeRange{}, panicRetCond); + Block *panic = &ifPanicRet.getThenRegion().emplaceBlock(); + b.setInsertionPointToStart(panic); + genStoreFlag(false); + // The clause's parameter is the panic code at offset 4. + BlockArgument arg = op.getPanicRegion().front().getArgument(0); + b.create(arg.getLoc(), /*dst=*/zero, + /*src=*/bExt.genI256Const(4), + /*size=*/bExt.genI256Const(0x20)); + mapLowered(arg, b.create(arg.getLoc(), zero)); + clauses.push_back( + {&op.getPanicRegion(), panic, &ifPanicRet.getThenRegion()}); + } + + if (!op.getErrorRegion().empty()) { + b.setInsertionPointToStart( + &switchOp.getCaseRegions()[caseIdx].emplaceBlock()); + b.setInsertionPoint(b.create(loc)); + + // Decode the ABI-encoded Error(string) like upstream's + // try_decode_error_message. Three nested guards; a failed one leaves + // the flag set so the fallback clause runs: + // + // - 0x44 = 4 (selector) + 0x20 (offset) + 0x20 (length) + // - 0x24 = 4 (selector) + 0x20 (offset) + // - offset is the ABI offset to the string body, read from the head. + // - length is the string length, read at msg. + // - dataEnd is one past the last byte of the returned tuple. + // + // clang-format off + // + // if (returndatasize >= 0x44) + // if (offset <= u64Max && offset + 0x24 <= returndatasize) + // if (length <= u64Max && msg + 0x20 + length <= dataEnd) + // run the Error clause + // + // clang-format on + Value sizeOk = bExt.genCmp(yul::CmpPredicate::uge, returnDataSize, + bExt.genI256Const(0x44)); + auto ifSizeOk = b.create(loc, TypeRange{}, sizeOk); + b.setInsertionPointToStart(&ifSizeOk.getThenRegion().emplaceBlock()); + b.setInsertionPoint(b.create(loc)); + Value u64Max = bExt.genI256Const(APInt::getLowBitsSet(256, 64)); + Value abiTupleSize = + b.create(loc, returnDataSize, bExt.genI256Const(4)); + Value abiTuple = evmB.genFreePtr(); + b.create(loc, /*dst=*/abiTuple, + /*src=*/bExt.genI256Const(4), abiTupleSize); + Value offset = b.create(loc, abiTuple); + Value offsetSmall = bExt.genCmp(yul::CmpPredicate::ule, offset, u64Max); + Value offsetEnd = + b.create(loc, offset, bExt.genI256Const(0x24)); + Value offsetInBounds = + bExt.genCmp(yul::CmpPredicate::ule, offsetEnd, returnDataSize); + Value offsetOk = b.create(loc, offsetSmall, offsetInBounds); + auto ifOffsetOk = b.create(loc, TypeRange{}, offsetOk); + b.setInsertionPointToStart(&ifOffsetOk.getThenRegion().emplaceBlock()); + b.setInsertionPoint(b.create(loc)); + + Value errMsg = b.create(loc, abiTuple, offset); + Value length = b.create(loc, errMsg); + Value lengthSmall = bExt.genCmp(yul::CmpPredicate::ule, length, u64Max); + Value msgEnd = b.create( + loc, b.create(loc, errMsg, bExt.genI256Const(0x20)), + length); + Value dataEnd = b.create(loc, abiTuple, abiTupleSize); + Value lengthInBounds = bExt.genCmp(yul::CmpPredicate::ule, msgEnd, dataEnd); + Value lengthOk = b.create(loc, lengthSmall, lengthInBounds); + auto ifLengthOk = b.create(loc, TypeRange{}, lengthOk); + Block *error = &ifLengthOk.getThenRegion().emplaceBlock(); + b.setInsertionPointToStart(error); + genStoreFlag(false); + // The copy is reserved only once every guard passed, and only up to the + // end of the reason. + evmB.genFreePtrUpd(abiTuple, b.create(loc, msgEnd, abiTuple)); + // The clause's parameter is the reason. + mapLowered(op.getErrorRegion().front().getArgument(0), errMsg); + clauses.push_back( + {&op.getErrorRegion(), error, &ifLengthOk.getThenRegion()}); + } + + // The fallback path runs if the flag is still set. + Block *fallbackPath = failure->splitBlock(fallbackPoint); + b.setInsertionPointAfter(ifSelectorRet); + Value runFallback = + b.create(loc, b.getIntegerType(256), runFallbackFlag, + evm::getAlignment(runFallbackFlag)); + auto ifRunFallback = b.create(loc, TypeRange{}, runFallback); + Block *fallback = &ifRunFallback.getThenRegion().emplaceBlock(); + fallback->getOperations().splice(fallback->end(), + fallbackPath->getOperations()); + fallbackPath->erase(); + b.create(loc); + if (!op.getFallbackRegion().empty()) + clauses.push_back( + {&op.getFallbackRegion(), fallback, &ifStatus.getElseRegion()}); + lowerClauses(clauses); } -LLVM::GlobalOp evm::StringLiteralPool::getOrCreate(StringRef literal, - sol::ContractOp contr, - OpBuilder &b, Location loc) { - auto [it, inserted] = globals.try_emplace(literal); - if (!inserted) - return it->second; - std::string name = ("__data_in_code." + Twine(globals.size() - 1)).str(); +void Lowering::lowerClauses(ArrayRef clauses) { OpBuilder::InsertionGuard guard(b); - b.setInsertionPointToStart(contr.getBody()); - it->second = b.create( - loc, LLVM::LLVMArrayType::get(b.getIntegerType(8), literal.size()), - /*isConstant=*/true, LLVM::Linkage::Private, name, - b.getStringAttr(literal), /*alignment=*/0, /*addrSpace=*/4); - it->second.setUnnamedAddr(LLVM::UnnamedAddr::Global); - return it->second; + for (const Clause &clause : clauses) { + b.setInsertionPointToEnd(clause.entry); + lowerBlock(clause.src->front()); + for (Block &block : llvm::drop_begin(*clause.src)) { + b.createBlock(clause.rest, clause.rest->end()); + lowerBlock(block); + } + } } -void evm::populateMemPats(RewritePatternSet &pats, TypeConverter &tyConv, - evm::StringLiteralPool &literals) { - pats.add( - tyConv, pats.getContext()); - pats.add(tyConv, pats.getContext(), - literals); - pats.add(pats.getContext()); +//===----------------------------------------------------------------------===// +// Internal function pointers +//===----------------------------------------------------------------------===// + +Value Lowering::lower(sol::FuncConstantOp op) { + Func target = getFunc(op.getSymAttr().getAttr()); + assert(target.id && "function constant of a function without an id"); + int64_t id = *target.id; + dispatch.addTarget(target.fn, id); + return solgen::BuilderExt(b, op.getLoc()).genI256Const(id); } -void evm::populateControlFlowPats(RewritePatternSet &pats, - TypeConverter &tyConv) { - pats.add(tyConv, pats.getContext()); +Value Lowering::lower(sol::DefaultFuncConstantOp op) { + return solgen::BuilderExt(b, op.getLoc()).genI256Const(0); } -void evm::populateFuncBoundaryPats(RewritePatternSet &pats, - TypeConverter &tyConv) { - pats.add(tyConv, pats.getContext()); +SmallVector Lowering::lower(sol::ICallOp op) { + Value callee = lowered(op.getCallee()); + SmallVector args = lowered(op.getOperands().drop_front()); + auto resTys = llvm::map_to_vector(op.getResultTypes(), evm::getYulType); + // The dispatch takes the pointer ahead of the callee's arguments. + SmallVector dispatchInputTys{callee.getType()}; + llvm::append_range(dispatchInputTys, ValueRange(args).getTypes()); + auto dispatchFnTy = + FunctionType::get(b.getContext(), dispatchInputTys, resTys); + args.insert(args.begin(), callee); + return b + .create( + op.getLoc(), resTys, + FlatSymbolRefAttr::get( + dispatch.getOrCreateDispatchFn(dispatchFnTy, curFn, b)), + args) + .getResults(); } -void evm::populateFuncOpPats(RewritePatternSet &pats, TypeConverter &tyConv) { - pats.add(tyConv, pats.getContext()); +//===----------------------------------------------------------------------===// +// Inline assembly +//===----------------------------------------------------------------------===// + +/// Returns whether `op` has a memory effect in upstream solc's sense +/// (InlineAssemblyAnnotation::hasMemoryEffects): a builtin that reads or +/// writes memory, or an assignment to a Solidity memory variable. +static bool hasMemoryEffects(Operation *op) { + if (auto effectOp = dyn_cast(op)) { + SmallVector effects; + effectOp.getEffectsOnResource(yul::MemoryResource::get(), effects); + if (!effects.empty()) + return true; + } + + auto store = dyn_cast(op); + if (!store) + return false; + auto ptrCast = store.getPtr().getDefiningOp(); + if (!ptrCast) + return false; + auto ptrTy = cast(ptrCast.getSrc().getType()); + return sol::getDataLocation(ptrTy.getPointeeType()) == + sol::DataLocation::Memory; } -void evm::populateAddrPat(RewritePatternSet &pats, TypeConverter &tyConv) { - pats.add( - pats.getContext()); - pats.add(tyConv, pats.getContext()); - pats.add( - tyConv, pats.getContext()); +/// Returns whether `op` is memory-unsafe: not annotated memory-safe, and its +/// body has a memory effect. +static bool isMemoryUnsafe(sol::InlineAsmOp op) { + if (op.getMemorySafe()) + return false; + return op.getBody() + .walk([](Operation *inner) { + return hasMemoryEffects(inner) ? WalkResult::interrupt() + : WalkResult::advance(); + }) + .wasInterrupted(); } -void evm::populateAbiPats(mlir::RewritePatternSet &pats, - mlir::TypeConverter &tyConv, - evm::StringLiteralPool &literals) { - pats.add(tyConv, pats.getContext()); - pats.add(tyConv, pats.getContext(), literals); +void Lowering::lower(sol::InlineAsmOp op) { + // A memory-unsafe body leaves out the contract's memoryguard. + if (!contr.getUnsafeAsm() && isMemoryUnsafe(op)) + contr.setUnsafeAsm(true); + + // The body's functions join the contract's, renamed apart from them. + MLIRContext *ctx = b.getContext(); + Block &body = op.getBody().front(); + auto fns = llvm::to_vector(body.getOps()); + for (yul::FuncOp fn : fns) { + StringAttr oldName = fn.getSymNameAttr(); + if (!SymbolTable::lookupSymbolIn(contr, oldName)) + continue; + unsigned counter = 0; + StringAttr uniqName = StringAttr::get( + ctx, SymbolTable::generateSymbolName<64>( + oldName.getValue(), + [&](StringRef cand) { + auto candName = StringAttr::get(ctx, cand); + return SymbolTable::lookupSymbolIn(contr, candName) || + SymbolTable::lookupSymbolIn(op, candName); + }, + counter)); + [[maybe_unused]] LogicalResult replaced = + SymbolTable::replaceAllSymbolUses(oldName, uniqName, op); + assert(succeeded(replaced)); + SymbolTable::setSymbolName(fn, uniqName); + } + // A function may read a Solidity constant, so its body is lowered too. + for (yul::FuncOp fn : fns) { + OpBuilder::InsertionGuard guard(b); + b.setInsertionPointToStart(contr.getBody()); + lowerRegions(fn, b.cloneWithoutRegions(*fn.getOperation(), valMap)); + fn.erase(); + } + + lowerBlock(body); } -void evm::populateExtCallPat(RewritePatternSet &pats, TypeConverter &tyConv, - evm::StringLiteralPool &literals) { - pats.add(tyConv, pats.getContext()); - pats.add(tyConv, pats.getContext(), - literals); +Value Lowering::getYulPtr(Value ptr, Location loc) { + if (isa(ptr.getType())) + return ptr; + // The Yul to LLVM IR translation reads an !llvm.ptr as a !yul.ptr through a + // builtin.unrealized_conversion_cast. + return b + .create( + loc, yul::PtrType::get(b.getContext()), ptr) + .getResult(0); } -void evm::populateEmitPat(RewritePatternSet &pats, TypeConverter &tyConv, - evm::StringLiteralPool &literals) { - pats.add(tyConv, pats.getContext(), - literals); +template +Value Lowering::lowerPairFieldPtr(OpT op, int field) { + Location loc = op.getLoc(); + auto i256Ty = b.getIntegerType(256); + auto pairTy = + LLVM::LLVMStructType::getLiteral(b.getContext(), {i256Ty, i256Ty}); + Value fieldPtr = b.create( + loc, LLVM::LLVMPointerType::get(b.getContext()), pairTy, + lowered(op.getSrc()), + ArrayRef{LLVM::GEPArg(0), LLVM::GEPArg(field)}); + return getYulPtr(fieldPtr, loc); } -void evm::populateConstantStringPat(RewritePatternSet &pats) { - pats.add(pats.getContext()); +// The `.offset` of a local storage reference is 0: its pointee is a reference +// type, the only kind Solidity admits there. +Value Lowering::lower(sol::YulStorageOffsetOp op) { + return solgen::BuilderExt(b, op.getLoc()).genI256Const(0); } -void evm::populateRequirePat(RewritePatternSet &pats, - evm::StringLiteralPool &literals) { - pats.add(pats.getContext()); - pats.add(pats.getContext(), literals); +Value Lowering::lower(sol::YulStateVarSlotOp op) { + auto stateVar = + contr.lookupSymbol(op.getSymAttr().getValue()); + return solgen::BuilderExt(b, op.getLoc()) + .genI256Const(stateVar.getSlot().zextOrTrunc(256)); } -void evm::populateContractPat(RewritePatternSet &pats, - const TypeConverter &tyConv) { - pats.add(pats.getContext(), tyConv); +Value Lowering::lower(sol::YulStateVarOffsetOp op) { + auto stateVar = + contr.lookupSymbol(op.getSymAttr().getValue()); + return solgen::BuilderExt(b, op.getLoc()) + .genI256Const(APInt(/*numBits=*/256, stateVar.getByteOffset())); } //===----------------------------------------------------------------------===// -// Inline-asm wrapper + yul.* bridge / pointer-plumbing op lowerings. +// Contract //===----------------------------------------------------------------------===// -namespace { +/// The immutable a library's runtime code compares its address with. +static constexpr const char *libAddrName = "library_deploy_address"; -struct InlineAsmOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - /// Upstream solc's InlineAssemblyAnnotation::hasMemoryEffects: a builtin - /// that reads or writes memory, or an assignment to a Solidity memory - /// variable. - static bool hasMemoryEffects(Operation *op) { - if (auto effectOp = dyn_cast(op)) { - SmallVector effects; - effectOp.getEffectsOnResource(yul::MemoryResource::get(), effects); - if (!effects.empty()) - return true; - } +yul::FuncCallOp Lowering::genCall(StringAttr callee, ValueRange args, + Location loc) { + return b.create(loc, getFunc(callee).fn, args); +} - auto store = dyn_cast(op); - if (!store) - return false; - auto ptrCast = store.getPtr().getDefiningOp(); - if (!ptrCast) - return false; - auto ptrTy = cast(ptrCast.getSrc().getType()); - return sol::getDataLocation(ptrTy.getPointeeType()) == - sol::DataLocation::Memory; - } +void Lowering::genCallValChk(Location loc) { + solgen::BuilderExt bExt(b, loc); + auto callVal = b.create(loc); + auto callValChk = + bExt.genCmp(yul::CmpPredicate::ne, callVal, bExt.genI256Const(0)); + evmBuilder(loc).genDebugRevertWithMsg( + callValChk, "Ether sent to non-payable function", loc); +} - /// Whether `op` is memory-unsafe: not annotated memory-safe, and its body - /// has a memory effect. - static bool isMemoryUnsafe(sol::InlineAsmOp op) { - if (op.getMemorySafe()) - return false; - return op.getBody() - .walk([](Operation *inner) { - return hasMemoryEffects(inner) ? WalkResult::interrupt() - : WalkResult::advance(); - }) - .wasInterrupted(); - } +void Lowering::genFreePtrInit(Location loc, size_t reservedMem) { + solgen::BuilderExt bExt(b, loc); + int64_t start = + mlir::evm::MemoryLayout::generalPurposeMemoryStart + reservedMem; + Value freePtr; + if (contr.getUnsafeAsm()) + freePtr = bExt.genI256Const(start); + else + freePtr = b.create( + loc, b.getIntegerAttr(b.getIntegerType(256), start)); + b.create(loc, bExt.genI256Const(64), freePtr); +} - LogicalResult matchAndRewrite(sol::InlineAsmOp op, OpAdaptor, - ConversionPatternRewriter &r) const override { - Operation *dest = SymbolTable::getNearestSymbolTable(op->getParentOp()); - assert(dest && "inline_asm must be within a symbol table"); - Block &destBlock = dest->getRegion(0).front(); - Block &block = op.getBody().front(); - MLIRContext *ctx = r.getContext(); - - // The contract's lowering, a later stage, leaves out the memoryguard of a - // marked contract. - auto contr = op->getParentOfType(); - assert(contr && "inline_asm must be within a sol.contract"); - if (!contr.getUnsafeAsm() && isMemoryUnsafe(op)) - r.modifyOpInPlace(contr, [&] { contr.setUnsafeAsm(true); }); - - auto fns = llvm::to_vector(block.getOps()); - - for (yul::FuncOp fn : fns) { - StringAttr oldName = fn.getSymNameAttr(); - if (!SymbolTable::lookupSymbolIn(dest, oldName)) - continue; - - // Uniquify the name - unsigned counter = 0; - StringAttr uniqName = StringAttr::get( - ctx, SymbolTable::generateSymbolName<64>( - oldName.getValue(), - [&](StringRef cand) { - auto a = StringAttr::get(ctx, cand); - return SymbolTable::lookupSymbolIn(dest, a) || - SymbolTable::lookupSymbolIn(op, a); - }, - counter)); - if (failed(SymbolTable::replaceAllSymbolUses(oldName, uniqName, op))) - return failure(); - SymbolTable::setSymbolName(fn, uniqName); +void Lowering::genDispatch(ContractFns &fns, Location loc) { + solgen::BuilderExt bExt(b, loc); + bool isLibrary = contr.getKind() == sol::ContractKind::Library; + + Value notDelegateCallCond; + if (isLibrary) { + auto libAddr = + b.create(loc, b.getIntegerType(256), libAddrName); + auto currAddr = b.create(loc); + notDelegateCallCond = bExt.genCmp(yul::CmpPredicate::eq, libAddr, currAddr); + } + + if (fns.ifc.empty()) + return; + + // One call value test serves the whole switch when no route reachable + // from it accepts value: the cases are the interface functions, and the + // default falls through to the fallback. A receive function is only + // reached with empty calldata, outside the `calldatasize >= 4` block, so + // it does not count. A library emits no test, its functions cannot be + // payable. + auto isPayable = [](sol::FuncOp fn) { + return fn.getStateMutability() == sol::StateMutability::Payable; + }; + bool hoistCallValChk = !isLibrary && llvm::none_of(fns.ifc, isPayable) && + (!fns.fallback || !isPayable(fns.fallback)); + + auto callDataSz = b.create(loc); + auto callDataSzCmp = + bExt.genCmp(yul::CmpPredicate::uge, callDataSz, bExt.genI256Const(4)); + auto ifOp = bExt.createIf(callDataSzCmp); + OpBuilder::InsertionGuard insertGuard(b); + b.setInsertionPointToStart(&ifOp.getThenRegion().front()); + + if (hoistCallValChk) + genCallValChk(loc); + + auto callDataLd = b.create(loc, bExt.genI256Const(0)); + Value callDataSelector = + b.create(loc, callDataLd, bExt.genI256Const(224)); + + SmallVector selectorValues; + for (uint32_t selector : fns.selectors) + selectorValues.emplace_back(/*numBits=*/256, selector); + auto selectorsAttr = DenseIntElementsAttr::get( + RankedTensorType::get(static_cast(fns.selectors.size()), + b.getIntegerType(256)), + selectorValues); + auto switchOp = b.create(loc, /*resultTypes=*/TypeRange{}, + callDataSelector, selectorsAttr, + fns.selectors.size()); + + { + OpBuilder::InsertionGuard insertGuard(b); + b.setInsertionPointToStart(b.createBlock(&switchOp.getDefaultRegion())); + b.create(loc); + } + + for (auto [caseRegion, ifcFn] : + llvm::zip(switchOp.getCaseRegions(), fns.ifc)) { + OpBuilder::InsertionGuard insertGuard(b); + b.setInsertionPointToStart(b.createBlock(&caseRegion)); + + // A case runs its interface function, so it is at that function's + // location. + Location fnLoc = evm::sourceLoc(ifcFn.getLoc()); + solgen::BuilderExt fnBExt(b, fnLoc); + evm::Builder fnEvmB = evmBuilder(fnLoc); + + assert(ifcFn.getStateMutability()); + sol::StateMutability stateMutability = *ifcFn.getStateMutability(); + if (isLibrary) { + assert(stateMutability != sol::StateMutability::Payable); + if (stateMutability > sol::StateMutability::View) { + assert(notDelegateCallCond); + fnEvmB.genDebugRevertWithMsg( + notDelegateCallCond, + "Non-view function of library called without DELEGATECALL", fnLoc); + } } - for (yul::FuncOp fn : fns) - r.moveOpBefore(fn, &destBlock, destBlock.begin()); - - // The Solidity code after a body that halts is unreachable. - if (!block.empty() && block.back().hasTrait()) - r.eraseBlock(r.splitBlock(op->getBlock(), std::next(op->getIterator()))); - r.inlineBlockBefore(&block, op); - r.eraseOp(op); - return success(); + if (!hoistCallValChk && !isLibrary && + stateMutability != sol::StateMutability::Payable) + genCallValChk(fnLoc); + + FunctionType origIfcFnTy = *ifcFn.getOrigFnType(); + SmallVector abiInputTys = getABITypes( + b, origIfcFnTy.getInputs(), origIfcFnTy.getInputs(), isLibrary); + SmallVector abiResultTys = getABITypes( + b, origIfcFnTy.getResults(), origIfcFnTy.getResults(), isLibrary); + std::vector decodedArgs; + if (!abiInputTys.empty()) + fnEvmB.genABITupleDecoding(abiInputTys, + /*tupleStart=*/fnBExt.genI256Const(4), + /*tupleEnd=*/callDataSz, decodedArgs, + /*fromMem=*/false); + + auto callOp = genCall(ifcFn.getSymNameAttr(), decodedArgs, fnLoc); + + auto tupleStart = fnEvmB.genFreePtr(); + Value tupleSize; + if (!callOp.getResultTypes().empty()) { + auto tupleEnd = + fnEvmB.genABIEncoding(abiResultTys, callOp.getResults(), tupleStart); + tupleSize = b.create(fnLoc, tupleEnd, tupleStart); + } else { + tupleSize = fnBExt.genI256Const(0); + } + b.create(fnLoc, tupleStart, tupleSize); } -}; +} -// sol.yul_ptr_cast / sol.yul_val_cast / sol.yul_storage_slot -template -struct IdentityCastLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; +void Lowering::genCreationCode(sol::FuncOp ctor, StringRef runtimeObjName, + Location loc) { + solgen::BuilderExt bExt(b, loc); + SmallVector immOps( + contr.getBody()->getOps()); - LogicalResult matchAndRewrite(OpT op, typename OpT::Adaptor adaptor, - ConversionPatternRewriter &r) const override { - r.replaceOp(op, adaptor.getSrc()); - return success(); - } -}; + genFreePtrInit(loc, immOps.size() * evm::MemoryLayout::immutableCellSize); -// Generates gep to a struct field through an opaque !llvm.ptr operand. -static Value genStructFieldGep(ConversionPatternRewriter &r, Location loc, - Value structPtr, LLVM::LLVMStructType structTy, - int field) { - auto ptrTy = LLVM::LLVMPointerType::get(r.getContext()); - return r.create( - loc, ptrTy, structTy, structPtr, - ArrayRef{LLVM::GEPArg(0), LLVM::GEPArg(field)}); + // The constructor's check, argument decoding and call run the constructor, + // so they are at its location. + Location ctorLoc = ctor ? evm::sourceLoc(ctor.getLoc()) : loc; + evm::Builder ctorEvmB = evmBuilder(ctorLoc); + + if (!ctor) { + genCallValChk(loc); + } else { + assert(ctor.getStateMutability()); + if (*ctor.getStateMutability() != sol::StateMutability::Payable) + genCallValChk(ctorLoc); + } + + if (ctor && contr.getKind() != sol::ContractKind::Library) { + std::vector decodedArgs; + FunctionType ctorFnTy = *ctor.getOrigFnType(); + if (!ctorFnTy.getInputs().empty()) { + auto progSize = b.create(ctorLoc, contr.getName()); + auto codeSize = b.create(ctorLoc); + auto argSize = b.create(ctorLoc, codeSize, progSize); + Value tupleStart = ctorEvmB.genMemAlloc(argSize); + b.create(ctorLoc, tupleStart, progSize, argSize); + ctorEvmB.genABITupleDecoding( + ctorFnTy.getInputs(), tupleStart, + /*tupleEnd=*/b.create(ctorLoc, tupleStart, argSize), + decodedArgs, /*fromMem=*/true); + } + genCall(ctor.getSymNameAttr(), decodedArgs, ctorLoc); + } + + auto freePtr = b.create(loc, bExt.genI256Const(64)); + auto runtimeObjSym = FlatSymbolRefAttr::get(b.getContext(), runtimeObjName); + auto runtimeObjOffset = b.create(loc, runtimeObjSym); + auto runtimeObjSize = b.create(loc, runtimeObjSym); + b.create(loc, freePtr, runtimeObjOffset, runtimeObjSize); + + // The runtime code of a library compares its address with this one to + // reject a call that is not a delegate call. + if (contr.getKind() == sol::ContractKind::Library) + b.create(loc, freePtr, libAddrName, + b.create(loc)); + + for (sol::ImmutableOp immOp : immOps) { + auto addr = bExt.genI256Const(evm::getImmutableAddr(immOp)); + auto val = b.create(loc, addr); + b.create(loc, freePtr, immOp.getName(), val); + } + + b.create(loc, freePtr, runtimeObjSize); } -// sol.yul_calldata_offset / sol.yul_calldata_length -template -struct CallDataMemberLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(OpT op, typename OpT::Adaptor adaptor, - ConversionPatternRewriter &r) const override { - auto i256Ty = IntegerType::get(r.getContext(), 256, - IntegerType::SignednessSemantics::Signless); - auto structTy = - LLVM::LLVMStructType::getLiteral(r.getContext(), {i256Ty, i256Ty}); - Value gep = - genStructFieldGep(r, op.getLoc(), adaptor.getSrc(), structTy, Field); - r.replaceOp(op, gep); - return success(); +void Lowering::genRuntimeCode(ContractFns &fns, Location loc) { + solgen::BuilderExt bExt(b, loc); + evm::Builder evmB = evmBuilder(loc); + genFreePtrInit(loc); + genDispatch(fns, loc); + + if (fns.receive) { + auto callDataSz = b.create(loc); + auto callDataSzIsZero = + bExt.genCmp(yul::CmpPredicate::eq, callDataSz, bExt.genI256Const(0)); + OpBuilder::InsertionGuard insertGuard(b); + auto ifOp = b.create(loc, TypeRange{}, callDataSzIsZero); + b.setInsertionPointToStart(&ifOp.getThenRegion().emplaceBlock()); + genCall(fns.receive.getSymNameAttr(), /*args=*/{}, loc); + b.create(loc); + } + + if (!fns.fallback) { + evmB.genDebugRevertWithMsg( + fns.receive ? "Unknown signature and no fallback defined" + : "Contract does not have fallback nor receive functions", + loc); + return; } -}; -// sol.yul_selector / sol.yul_address_of -template -struct ExtFuncMemberLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(OpT op, typename OpT::Adaptor adaptor, - ConversionPatternRewriter &r) const override { - auto i256Ty = IntegerType::get(r.getContext(), 256, - IntegerType::SignednessSemantics::Signless); - auto structTy = - LLVM::LLVMStructType::getLiteral(r.getContext(), {i256Ty, i256Ty}); - Value gep = - genStructFieldGep(r, op.getLoc(), adaptor.getSrc(), structTy, Field); - r.replaceOp(op, gep); - return success(); - } -}; + FunctionType fallbackFnTy = fns.fallback.getFunctionType(); + // Both valid fallback forms (with and without bytes) match counts. + assert(fallbackFnTy.getNumInputs() == fallbackFnTy.getNumResults() && + "fallback has mismatched parameter/return counts"); -// sol.yul_storage_offset: local storage ref `.offset` is always 0 for ref-type -// pointees (the only kind of local storage ref Solidity admits). -struct StorageOffsetOpLowering - : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::YulStorageOffsetOp op, OpAdaptor, - ConversionPatternRewriter &r) const override { - mlir::solgen::BuilderExt bExt(r, op.getLoc()); - r.replaceOp(op, bExt.genI256Const(0)); - return success(); - } -}; + if (fns.fallback.getStateMutability() != sol::StateMutability::Payable) + genCallValChk(loc); -// sol.yul_state_var_slot / sol.yul_state_var_offset -template -struct StateVarFieldLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(OpT op, typename OpT::Adaptor, - ConversionPatternRewriter &r) const override { - auto contract = op->template getParentOfType(); - assert(contract); - - auto sym = contract.template lookupSymbol( - op.getSymAttr().getValue()); - assert(sym); - - mlir::solgen::BuilderExt bExt(r, op.getLoc()); - APInt value = IsOffset ? APInt(/*numBits=*/256, sym.getByteOffset()) - : sym.getSlot().zextOrTrunc(256); - r.replaceOp(op, bExt.genI256Const(value)); - return success(); + // The `bytes calldata` parameter, when present, is the full call payload: + // a `{offset, length}` reference of `{0, calldatasize()}`. + SmallVector fallbackArgs; + if (fallbackFnTy.getNumInputs() == 1) { + Value callDataSz = b.create(loc); + fallbackArgs.push_back( + bExt.genLLVMStruct({bExt.genI256Const(0), callDataSz}, loc)); } -}; -} // namespace + auto call = genCall(fns.fallback.getSymNameAttr(), fallbackArgs, loc); -void evm::populateInlineAsmPats(RewritePatternSet &pats, - TypeConverter &tyConv) { - pats.add, - IdentityCastLowering, - IdentityCastLowering, - CallDataMemberLowering, - CallDataMemberLowering, - ExtFuncMemberLowering, - ExtFuncMemberLowering, - StorageOffsetOpLowering, - StateVarFieldLowering, - StateVarFieldLowering>( - tyConv, pats.getContext()); + if (fallbackFnTy.getNumResults() == 1) { + // The returned `bytes memory` is returned verbatim, not ABI-encoded like + // a dispatch result. + Value ret = call.getResult(0); + Value dataPtr = evmB.genDataAddrPtr(ret, sol::DataLocation::Memory, loc); + Value len = b.create(loc, ret); + b.create(loc, dataPtr, len); + } else { + b.create(loc); + } } -void evm::populateInternalFuncPtrPats(RewritePatternSet &pats, - TypeConverter &tyConv, - evm::InternalDispatch &dispatch) { - pats.add(pats.getContext()); - pats.add(pats.getContext(), dispatch); - pats.add(tyConv, pats.getContext(), dispatch); -} +void Lowering::lowerContract() { + // The code generated here is at the contract's location, except the + // dispatch cases and the constructor's part of the creation code, which are + // at their function's. + Location loc = contr.getLoc(); + + std::string runtimeObjName = std::string(contr.getName()) + "_deployed"; + b.setInsertionPoint(contr); + auto obj = b.create( + loc, contr.getRuntime() ? StringRef(runtimeObjName) : contr.getName()); + + ContractFns fns; + for (auto fn : contr.getBody()->getOps()) { + if (auto selector = fn.getSelector()) { + fns.selectors.push_back(*selector); + fns.ifc.push_back(fn); + } + if (fn.getKind() == sol::FunctionKind::Constructor) { + assert(!fns.ctor); + fns.ctor = fn; + } else if (fn.getKind() == sol::FunctionKind::Receive) { + assert(!fns.receive); + fns.receive = fn; + } else if (fn.getKind() == sol::FunctionKind::Fallback) { + assert(!fns.fallback); + fns.fallback = fn; + } + } + + StringRef entryName = yul::FuncOp::entryName; + b.setInsertionPointToStart(obj.getBody()); + auto entryFn = b.create( + evm::fuseArtificialSubprogram(mod, entryName, mod.getLoc()), entryName, + b.getFunctionType({}, {})); + b.createBlock(&entryFn.getBody()); + if (contr.getRuntime()) + genRuntimeCode(fns, loc); + else + genCreationCode(fns.ctor, runtimeObjName, loc); + + for (auto fn : + llvm::make_early_inc_range(contr.getBody()->getOps())) + fn.erase(); + + // The functions and the string literal globals follow the entry function, + // the last one first, which keeps the layout of the object and so its + // bytecode. + for (Operation &op : + llvm::make_early_inc_range(llvm::reverse(*contr.getBody()))) { + if (isa(op)) + op.moveBefore(obj.getBody(), obj.getBody()->end()); + else + assert((isa(op))); + } -void evm::populateStage1Pats(RewritePatternSet &pats, TypeConverter &tyConv, - evm::InternalDispatch &dispatch, - evm::StringLiteralPool &literals) { - populateArithPats(pats, tyConv); - populateInternalFuncPtrPats(pats, tyConv, dispatch); - populateCheckedArithPats(pats, tyConv); - populateCryptoPats(pats, tyConv, literals); - populateMemPats(pats, tyConv, literals); - populateAddrPat(pats, tyConv); - populateAbiPats(pats, tyConv, literals); - populateExtCallPat(pats, tyConv, literals); - populateEmitPat(pats, tyConv, literals); - populateRequirePat(pats, literals); - populateControlFlowPats(pats, tyConv); - populateInlineAsmPats(pats, tyConv); + contr.erase(); } -void evm::populateStage2Pats(RewritePatternSet &pats, - const TypeConverter &tyConv) { - populateContractPat(pats, tyConv); -} +//===----------------------------------------------------------------------===// +// Pass +//===----------------------------------------------------------------------===// + +namespace { + +struct ConvertSolToYulPass + : public impl::ConvertSolToYulPassBase { + void runOnOperation() override { + ModuleOp mod = getOperation(); + // The module lowers to the object of its one contract, and a nested + // module's contract would lower without its creation func refs. + auto contracts = mod.getOps(); + if (!llvm::hasSingleElement(contracts) || !mod.getOps().empty()) { + mod.emitOpError("expects one sol.contract and no nested module"); + signalPassFailure(); + return; + } + Lowering(*contracts.begin()).run(); + } +}; + +} // namespace diff --git a/mlir/lib/Conversion/SolToYul/SolToYulPass.cpp b/mlir/lib/Conversion/SolToYul/SolToYulPass.cpp deleted file mode 100644 index cdb0475918b1..000000000000 --- a/mlir/lib/Conversion/SolToYul/SolToYulPass.cpp +++ /dev/null @@ -1,315 +0,0 @@ -//===- SolToYulPass.cpp - Sol to Yul dialect lowering pass ----------------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// -// -// Lowers the sol dialect to the yul dialect (with func/LLVM helpers). -// -//===----------------------------------------------------------------------===// - -#include "mlir/Conversion/SolToYul/EVMUtil.h" -#include "mlir/Conversion/SolToYul/SolToYul.h" -#include "mlir/Dialect/LLVMIR/LLVMDialect.h" -#include "mlir/Dialect/Sol/Sol.h" -#include "mlir/Dialect/Yul/Yul.h" -#include "mlir/Transforms/DialectConversion.h" - -namespace mlir { -#define GEN_PASS_DEF_CONVERTSOLTOYULPASS -#include "mlir/Conversion/Passes.h.inc" -} // namespace mlir - -using namespace mlir; - -namespace { - -/// A generic conversion pattern that replaces the operands with the legalized -/// ones and legalizes the return types. -template -struct GenericTypeConversion : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(OpT op, typename OpT::Adaptor adaptor, - ConversionPatternRewriter &r) const override { - SmallVector retTys; - if (failed(this->getTypeConverter()->convertTypes(op->getResultTypes(), - retTys))) - return failure(); - - r.replaceOpWithNewOp(op, retTys, adaptor.getOperands(), - op->getAttrs()); - return success(); - } -}; - -struct ConvCastOpLowering : public OpConversionPattern { - using OpConversionPattern::OpConversionPattern; - - LogicalResult matchAndRewrite(sol::ConvCastOp op, OpAdaptor adaptor, - ConversionPatternRewriter &r) const override { - r.replaceOp(op, adaptor.getInp()); - return success(); - } -}; - -/// Pass for lowering the sol dialect to the yul dialect. -struct ConvertSolToYulPass - : public impl::ConvertSolToYulPassBase { - - // TODO: Generalize this comment. - // - // FIXME: Some of the conversion patterns depends on the ancestor/descendant - // sol.func ops (e.g.: placing a dispatch after its caller). Also APIs - // like getOrInsert*FuncOp that are used in the conversion pass works with - // sol.func. sol.func conversion in the same conversion pass will require - // other conversion to be able to work with sol.func and func.func. To keep - // things simple for now, the sol.func and related ops lowering is scheduled - // in a separate conversion pass after the main one. - // - // How can we do the conversion cleanly with one pass? Generally speaking, - // how should we work with conversion patterns that depends on other - // operations? - - // FIXME: Separate yul specific sol ops to a "yul" dialect? This could - // simplify the implementation of this multi-staged lowering. - - /// Converts sol dialect ops except sol.contract and sol.func + related ops. - /// This pass also legalizes all the sol dialect types. - LogicalResult runStage1Conversion(ModuleOp mod, evm::SolTypeConverter &tyConv, - evm::InternalDispatch &dispatch, - evm::StringLiteralPool &literals) { - OpBuilder b(mod.getContext()); - - ConversionTarget convTgt(getContext()); - convTgt.addLegalOp(); - convTgt - .addLegalDialect(); - convTgt.addIllegalOp< - // clang-format off - sol::ConstantOp, - sol::FuncConstantOp, - sol::ICallOp, - sol::DefaultFuncConstantOp, - sol::ExtFuncConstantOp, - sol::ExtFuncAddrOp, - sol::ExtFuncSelectorOp, - sol::CastOp, - sol::AddressCastOp, - sol::ContractCastOp, - sol::EnumCastOp, - sol::BytesCastOp, - sol::DynBytesToFixedBytesOp, - sol::FixedBytesIndexOp, - sol::AddOp, - sol::SubOp, - sol::MulOp, - sol::DivOp, - sol::ModOp, - sol::AndOp, - sol::OrOp, - sol::XorOp, - sol::NotOp, - sol::ShlOp, - sol::ShrOp, - sol::CmpOp, - sol::CAddOp, - sol::CSubOp, - sol::CMulOp, - sol::CDivOp, - sol::AddModOp, - sol::MulModOp, - sol::ExpOp, - sol::Keccak256Op, - sol::Erc7201Op, - sol::Sha256Op, - sol::Ripemd160Op, - sol::EcrecoverOp, - sol::CExpOp, - sol::AllocaOp, - sol::MallocOp, - sol::ArrayLitOp, - sol::StringLitOp, - sol::GetCallDataOp, - sol::DefaultCallDataOp, - sol::DefaultStorageOp, - sol::PushOp, - sol::PushStringOp, - sol::PopOp, - sol::AddrOfOp, - sol::GepOp, - sol::MapOp, - sol::CopyOp, - sol::DeleteOp, - sol::DataLocCastOp, - sol::LoadOp, - sol::LoadImmutableOp, - sol::StoreOp, - sol::LengthOp, - sol::SliceOp, - sol::ThisOp, - sol::CallerOp, - sol::LibAddrOp, - sol::CodeHashOp, - sol::BalanceOp, - sol::EncodeOp, - sol::DecodeOp, - sol::ExtCallOp, - sol::BareCallOp, - sol::BareDelegateCallOp, - sol::BareStaticCallOp, - sol::SendOp, - sol::TransferOp, - sol::ExtICallOp, - sol::NewOp, - sol::ObjectCodeOp, - sol::CodeOp, - sol::CodeSizeOp, - sol::OriginOp, - sol::GasPriceOp, - sol::CallValueOp, - sol::SigOp, - sol::BaseFeeOp, - sol::BlobBaseFeeOp, - sol::ChainIdOp, - sol::CoinbaseOp, - sol::DifficultyOp, - sol::GasLimitOp, - sol::BlockNumberOp, - sol::PrevRandaoOp, - sol::TimestampOp, - sol::GasLeftOp, - sol::BlockHashOp, - sol::BlobHashOp, - sol::SelfdestructOp, - sol::ConcatOp, - sol::RevertOp, - sol::EmitOp, - sol::RequireOp, - sol::AssertOp, - sol::ConvCastOp, - sol::IfOp, - sol::SwitchOp, - sol::WhileOp, - sol::DoWhileOp, - sol::ForOp, - sol::TryOp, - sol::ScopeOp, - sol::LeaveOp, - sol::InlineAsmOp, - sol::YulPtrCastOp, - sol::YulValCastOp, - sol::YulStorageSlotOp, - sol::YulCallDataOffsetOp, - sol::YulCallDataLengthOp, - sol::YulSelectorOp, - sol::YulFuncAddrOp, - sol::YulStorageOffsetOp, - sol::YulStateVarSlotOp, - sol::YulStateVarOffsetOp - // clang-format on - >(); - convTgt.addDynamicallyLegalOp([&](sol::FuncOp op) { - return tyConv.isSignatureLegal(op.getFunctionType()); - }); - convTgt.addDynamicallyLegalOp( - [&](Operation *op) { return tyConv.isLegal(op); }); - - RewritePatternSet pats(&getContext()); - pats.add(tyConv, &getContext()); - populateAnyFunctionOpInterfaceTypeConversionPattern(pats, tyConv); - pats.add, - GenericTypeConversion>(tyConv, &getContext()); - - evm::populateStage1Pats(pats, tyConv, dispatch, literals); - - if (failed(applyPartialConversion(mod, convTgt, std::move(pats)))) - return failure(); - return success(); - } - - /// Converts sol.contract to Yul. - LogicalResult runStage2Conversion(ModuleOp mod, - const evm::SolTypeConverter &tyConv) { - ConversionTarget convTgt(getContext()); - convTgt.addLegalOp(); - convTgt - .addLegalDialect(); - convTgt.addIllegalOp(); - - RewritePatternSet pats(&getContext()); - evm::populateStage2Pats(pats, tyConv); - - if (failed(applyPartialConversion(mod, convTgt, std::move(pats)))) - return failure(); - return success(); - } - - /// Converts sol.func and related function boundary ops to Yul. - LogicalResult runStage3Conversion(ModuleOp mod, - evm::SolTypeConverter &tyConv) { - // Keep sol.func legal while lowering sol.call/sol.return so function bodies - // are already Yul-clean before FuncOpLowering moves them into yul.func. - { - ConversionTarget convTgt(getContext()); - convTgt.addLegalOp(); - convTgt.addLegalDialect(); - convTgt.addIllegalOp(); - - RewritePatternSet pats(&getContext()); - evm::populateFuncBoundaryPats(pats, tyConv); - - if (failed(applyPartialConversion(mod, convTgt, std::move(pats)))) - return failure(); - } - - { - ConversionTarget convTgt(getContext()); - convTgt.addLegalOp(); - convTgt.addLegalDialect(); - convTgt.addIllegalDialect(); - - RewritePatternSet pats(&getContext()); - evm::populateFuncOpPats(pats, tyConv); - evm::populateConstantStringPat(pats); - - if (failed(applyPartialConversion(mod, convTgt, std::move(pats)))) - return failure(); - } - return success(); - } - - void runOnOperation() override { - ModuleOp mod = getOperation(); - evm::SolTypeConverter tyConv; - evm::InternalDispatch dispatch(tyConv); - evm::StringLiteralPool literals; - // The module lowers to the object of its one contract, and a nested - // module's contract would lower without its creation func refs. - auto contracts = mod.getOps(); - if (!llvm::hasSingleElement(contracts) || !mod.getOps().empty()) { - mod.emitOpError("expects one sol.contract and no nested module"); - signalPassFailure(); - return; - } - dispatch.addCreationFuncRefs(*contracts.begin()); - if (failed(runStage1Conversion(mod, tyConv, dispatch, literals))) { - signalPassFailure(); - return; - } - if (failed(runStage2Conversion(mod, tyConv))) { - signalPassFailure(); - return; - } - dispatch.genDispatchFns(); - if (failed(runStage3Conversion(mod, tyConv))) { - signalPassFailure(); - return; - } - } -}; - -} // namespace diff --git a/mlir/lib/Conversion/SolToYul/TypeConverter.cpp b/mlir/lib/Conversion/SolToYul/TypeConverter.cpp deleted file mode 100644 index e8f50f2ad68c..000000000000 --- a/mlir/lib/Conversion/SolToYul/TypeConverter.cpp +++ /dev/null @@ -1,217 +0,0 @@ -//===- TypeConverter.cpp - Sol dialect type converter ---------------------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// -// -// EVM specific type converter of the sol dialect. -// -//===----------------------------------------------------------------------===// - -#include "mlir/Conversion/SolToYul/EVMUtil.h" -#include "mlir/Conversion/SolToYul/SolToYul.h" -#include "mlir/Dialect/LLVMIR/LLVMTypes.h" -#include "mlir/Dialect/Sol/Sol.h" -#include "mlir/Dialect/Yul/Yul.h" -#include "mlir/IR/BuiltinOps.h" - -using namespace mlir; - -evm::SolTypeConverter::SolTypeConverter() { - // Default case - addConversion([](Type ty) { return ty; }); - - // Integer type - addConversion([&](IntegerType ty) -> Type { - // Sol-to-Yul promotes all Solidity integer-like scalar carriers, including - // bool, to the Yul word type. Individual lowering patterns still use the - // original Sol type to preserve signedness and width-specific cleanup. - return IntegerType::get(ty.getContext(), /*width=*/256, - IntegerType::Signless); - }); - - // Bytes type - addConversion([&](sol::FixedBytesType ty) -> Type { - return IntegerType::get(ty.getContext(), /*width=*/256, - IntegerType::Signless); - }); - - addConversion([&](sol::ByteType ty) -> Type { - return IntegerType::get(ty.getContext(), /*width=*/256, - IntegerType::Signless); - }); - - // Enum type - addConversion([&](sol::EnumType ty) -> Type { - // Unlike integer types, legalizing to i256 avoids unnecessary zext/trunc - // ops. - return IntegerType::get(ty.getContext(), 256, IntegerType::Signless); - }); - - // Address type - addConversion([&](sol::AddressType ty) -> Type { - return IntegerType::get(ty.getContext(), 256, IntegerType::Signless); - }); - - // Contract type - addConversion([&](sol::ContractType ty) -> Type { - return IntegerType::get(ty.getContext(), 256, IntegerType::Signless); - }); - - // Function ref type - addConversion([&](sol::FuncRefType ty) -> Type { - // Maps to the sol.func id used in the dispatch table generated during the - // indirect call lowering. - return IntegerType::get(ty.getContext(), 256, IntegerType::Signless); - }); - - // External function ref type - addConversion([&](sol::ExtFuncRefType ty) -> Type { - // {addr, selector} with each half LSB-aligned in its i256 lane. Mirrors - // upstream Yul's two-stack-item layout. Packing into a 32-byte word - // (MSB-aligned for memory/calldata/ABI, right-aligned 192 bits for - // storage) happens at the location boundary. - auto i256Ty = IntegerType::get(ty.getContext(), 256, IntegerType::Signless); - return LLVM::LLVMStructType::getLiteral(ty.getContext(), {i256Ty, i256Ty}); - }); - - // Function type - addConversion([&](FunctionType ty) -> Type { - SmallVector convertedInpTys, convertedResTys; - if (failed(convertTypes(ty.getInputs(), convertedInpTys))) - llvm_unreachable("Invalid type"); - if (failed(convertTypes(ty.getResults(), convertedResTys))) - llvm_unreachable("Invalid type"); - - return FunctionType::get(ty.getContext(), convertedInpTys, convertedResTys); - }); - - // Array type - addConversion([&](sol::ArrayType ty) -> Type { - auto i256Ty = IntegerType::get(ty.getContext(), 256, - IntegerType::SignednessSemantics::Signless); - switch (ty.getDataLocation()) { - case sol::DataLocation::Stack: { - Type eltTy = convertType(ty.getEltType()); - return LLVM::LLVMArrayType::get(eltTy, ty.getSize().getZExtValue()); - } - - // Map to fat pointer {addr, size}. - case sol::DataLocation::CallData: - if (ty.isDynSized()) - return LLVM::LLVMStructType::getLiteral(ty.getContext(), - {i256Ty, i256Ty}); - return i256Ty; - - // Map to memory address. - case sol::DataLocation::Memory: - return i256Ty; - - // Map to storage/transient slot. - case sol::DataLocation::Storage: - case sol::DataLocation::Transient: - return i256Ty; - - default: - break; - } - - llvm_unreachable("Unimplemented type conversion"); - }); - - // String type - addConversion([&](sol::StringType ty) -> Type { - auto i256Ty = IntegerType::get(ty.getContext(), 256, - IntegerType::SignednessSemantics::Signless); - switch (ty.getDataLocation()) { - // Map to fat pointer {addr, size}. - case sol::DataLocation::CallData: - return LLVM::LLVMStructType::getLiteral(ty.getContext(), - {i256Ty, i256Ty}); - - // Map to memory address. - case sol::DataLocation::Memory: - return i256Ty; - - // Map to storage/transient slot. - case sol::DataLocation::Storage: - case sol::DataLocation::Transient: - return i256Ty; - - default: - break; - } - - llvm_unreachable("Unimplemented type conversion"); - }); - - // Mapping type - addConversion([&](sol::MappingType ty) -> Type { - // Map to storage slot. - return IntegerType::get(ty.getContext(), 256, - IntegerType::SignednessSemantics::Signless); - }); - - // Struct type - addConversion([&](sol::StructType ty) -> Type { - auto i256Ty = IntegerType::get(ty.getContext(), 256, - IntegerType::SignednessSemantics::Signless); - switch (ty.getDataLocation()) { - // Map to calldata/memory address. - case sol::DataLocation::CallData: - case sol::DataLocation::Memory: - return i256Ty; - - // Map to storage/transient slot. - case sol::DataLocation::Storage: - case sol::DataLocation::Transient: - return i256Ty; - - default: - break; - } - - llvm_unreachable("Unimplemented type conversion"); - }); - - // Pointer type - addConversion([&](sol::PointerType ty) -> Type { - auto i256Ty = IntegerType::get(ty.getContext(), 256, - IntegerType::SignednessSemantics::Signless); - switch (ty.getDataLocation()) { - case sol::DataLocation::Stack: - return LLVM::LLVMPointerType::get(ty.getContext()); - - case sol::DataLocation::CallData: - return i256Ty; - - // Map to calldata/memory address. - case sol::DataLocation::Memory: - case sol::DataLocation::Immutable: - return i256Ty; - - // Map to fat pointer {slot, offset} for packable types, just slot - // otherwise. - case sol::DataLocation::Storage: - case sol::DataLocation::Transient: - if (sol::canBePacked(ty.getPointeeType())) - return LLVM::LLVMStructType::getLiteral(ty.getContext(), - {i256Ty, i256Ty}); - return i256Ty; - } - - llvm_unreachable("Unimplemented type conversion"); - }); - - auto castMaterialization = [](OpBuilder &b, Type resTy, ValueRange ins, - Location loc) -> Value { - assert(ins.size() == 1); - if (isa(resTy) || isa(ins[0].getType())) - return b.create(loc, resTy, ins).getResult(0); - return b.create(loc, resTy, ins); - }; - addSourceMaterialization(castMaterialization); - addTargetMaterialization(castMaterialization); -} diff --git a/mlir/lib/Dialect/Sol/SolOps.cpp b/mlir/lib/Dialect/Sol/SolOps.cpp index d07889b64e6c..13ab5170a7a1 100644 --- a/mlir/lib/Dialect/Sol/SolOps.cpp +++ b/mlir/lib/Dialect/Sol/SolOps.cpp @@ -32,12 +32,6 @@ using namespace mlir; using namespace mlir::sol; -//===----------------------------------------------------------------------===// -// ConvCastOp -//===----------------------------------------------------------------------===// - -OpFoldResult ConvCastOp::fold(FoldAdaptor adaptor) { return adaptor.getInp(); } - //===----------------------------------------------------------------------===// // CDivOp //===----------------------------------------------------------------------===// diff --git a/mlir/test/Conversion/SolToYul/inline-asm.mlir b/mlir/test/Conversion/SolToYul/inline-asm.mlir index 8af016ecd113..ce3ac74c0afe 100644 --- a/mlir/test/Conversion/SolToYul/inline-asm.mlir +++ b/mlir/test/Conversion/SolToYul/inline-asm.mlir @@ -5,8 +5,8 @@ module { // A storage reference's .offset folds to 0. // CHECK-LABEL: yul.func @storageOffset : (i256) -> i256 // CHECK: %[[O:.*]] = llvm.alloca - // CHECK: %[[OP:.*]] = builtin.unrealized_conversion_cast %[[O]] : !llvm.ptr to !yul.ptr // CHECK: %[[Z:.*]] = yul.constant 0 + // CHECK: %[[OP:.*]] = builtin.unrealized_conversion_cast %[[O]] : !llvm.ptr to !yul.ptr // CHECK: yul.store %[[Z]], %[[OP]] : i256, !yul.ptr sol.func @storageOffset(%arg0: !sol.array) -> ui256 { %0 = sol.alloca : !sol.ptr @@ -33,16 +33,16 @@ module { // CHECK: %[[D:.*]] = llvm.alloca %{{.*}} x !llvm.struct<(i256, i256)> // CHECK: llvm.store %[[ARG]], %[[D]] // CHECK: %[[O:.*]] = llvm.alloca - // CHECK: %[[OP:.*]] = builtin.unrealized_conversion_cast %[[O]] : !llvm.ptr to !yul.ptr // CHECK: %[[L:.*]] = llvm.alloca - // CHECK: %[[LP:.*]] = builtin.unrealized_conversion_cast %[[L]] : !llvm.ptr to !yul.ptr // CHECK: %[[DO:.*]] = llvm.getelementptr %[[D]][0, 0] // CHECK: %[[DOP:.*]] = builtin.unrealized_conversion_cast %[[DO]] // CHECK: %[[OV:.*]] = yul.load %[[DOP]] + // CHECK: %[[OP:.*]] = builtin.unrealized_conversion_cast %[[O]] : !llvm.ptr to !yul.ptr // CHECK: yul.store %[[OV]], %[[OP]] // CHECK: %[[DL:.*]] = llvm.getelementptr %[[D]][0, 1] // CHECK: %[[DLP:.*]] = builtin.unrealized_conversion_cast %[[DL]] // CHECK: %[[LV:.*]] = yul.load %[[DLP]] + // CHECK: %[[LP:.*]] = builtin.unrealized_conversion_cast %[[L]] : !llvm.ptr to !yul.ptr // CHECK: yul.store %[[LV]], %[[LP]] sol.func @calldataMembers(%arg0: !sol.string) -> (ui256, ui256) { %0 = sol.alloca : !sol.ptr, Stack> @@ -72,8 +72,8 @@ module { sol.contract @C { // CHECK-LABEL: yul.func @constantRef : () -> i256 // CHECK: %[[W:.*]] = llvm.alloca - // CHECK: %[[WP:.*]] = builtin.unrealized_conversion_cast %[[W]] : !llvm.ptr to !yul.ptr // CHECK: %[[C42:.*]] = yul.constant 42 + // CHECK: %[[WP:.*]] = builtin.unrealized_conversion_cast %[[W]] : !llvm.ptr to !yul.ptr // CHECK: yul.store %[[C42]], %[[WP]] : i256, !yul.ptr sol.func @constantRef() -> ui256 { %0 = sol.alloca : !sol.ptr @@ -97,18 +97,18 @@ module { // CHECK-LABEL: yul.func @extFuncMembers : (!llvm.struct<(i256, i256)>) -> (i256, i256) // CHECK: ^bb0(%[[ARG:.*]]: !llvm.struct<(i256, i256)>): // CHECK: %[[A:.*]] = llvm.alloca - // CHECK: %[[AP:.*]] = builtin.unrealized_conversion_cast %[[A]] : !llvm.ptr to !yul.ptr // CHECK: %[[S:.*]] = llvm.alloca - // CHECK: %[[SP:.*]] = builtin.unrealized_conversion_cast %[[S]] : !llvm.ptr to !yul.ptr // CHECK: %[[F:.*]] = llvm.alloca %{{.*}} x !llvm.struct<(i256, i256)> // CHECK: llvm.store %[[ARG]], %[[F]] // CHECK: %[[FA:.*]] = llvm.getelementptr %[[F]][0, 0] // CHECK: %[[FAP:.*]] = builtin.unrealized_conversion_cast %[[FA]] // CHECK: %[[AV:.*]] = yul.load %[[FAP]] + // CHECK: %[[AP:.*]] = builtin.unrealized_conversion_cast %[[A]] : !llvm.ptr to !yul.ptr // CHECK: yul.store %[[AV]], %[[AP]] // CHECK: %[[FS:.*]] = llvm.getelementptr %[[F]][0, 1] // CHECK: %[[FSP:.*]] = builtin.unrealized_conversion_cast %[[FS]] // CHECK: %[[SV:.*]] = yul.load %[[FSP]] + // CHECK: %[[SP:.*]] = builtin.unrealized_conversion_cast %[[S]] : !llvm.ptr to !yul.ptr // CHECK: yul.store %[[SV]], %[[SP]] sol.func @extFuncMembers(%arg0: !sol.ext_func_ref<() -> ()>) -> (ui256, ui256) { %0 = sol.alloca : !sol.ptr @@ -137,12 +137,12 @@ module { // .slot/.offset fold to the storage layout. // CHECK-LABEL: yul.func @slotOffset : () -> (i256, i256) // CHECK: %[[SL:.*]] = llvm.alloca -// CHECK: %[[S:.*]] = builtin.unrealized_conversion_cast %[[SL]] : !llvm.ptr to !yul.ptr // CHECK: %[[OL:.*]] = llvm.alloca -// CHECK: %[[O:.*]] = builtin.unrealized_conversion_cast %[[OL]] : !llvm.ptr to !yul.ptr // CHECK: %[[C1:.*]] = yul.constant 1 +// CHECK: %[[S:.*]] = builtin.unrealized_conversion_cast %[[SL]] : !llvm.ptr to !yul.ptr // CHECK: yul.store %[[C1]], %[[S]] // CHECK: %[[C2:.*]] = yul.constant 2 +// CHECK: %[[O:.*]] = builtin.unrealized_conversion_cast %[[OL]] : !llvm.ptr to !yul.ptr // CHECK: yul.store %[[C2]], %[[O]] #Contract = #sol #Pure = #sol @@ -172,12 +172,13 @@ module { // CHECK-LABEL: yul.func @extRef : (i256) -> i256 // CHECK: ^bb0(%[[ARG:.*]]: i256): // CHECK: %[[X:.*]] = llvm.alloca -// CHECK: %[[XP:.*]] = builtin.unrealized_conversion_cast %[[X]] : !llvm.ptr to !yul.ptr // CHECK: llvm.store %[[ARG]], %[[X]] -// CHECK: %[[V:.*]] = yul.load %[[XP]] : !yul.ptr -> i256 +// CHECK: %[[XL:.*]] = builtin.unrealized_conversion_cast %[[X]] : !llvm.ptr to !yul.ptr +// CHECK: %[[V:.*]] = yul.load %[[XL]] : !yul.ptr -> i256 // CHECK: %[[C1:.*]] = yul.constant 1 // CHECK: %[[ADD:.*]] = yul.add %[[V]], %[[C1]] -// CHECK: yul.store %[[ADD]], %[[XP]] : i256, !yul.ptr +// CHECK: %[[XS:.*]] = builtin.unrealized_conversion_cast %[[X]] : !llvm.ptr to !yul.ptr +// CHECK: yul.store %[[ADD]], %[[XS]] : i256, !yul.ptr // CHECK: %[[RET:.*]] = llvm.load %[[X]] // CHECK: yul.func_return %[[RET]] : i256 #Contract = #sol @@ -207,9 +208,9 @@ module { // CHECK-LABEL: yul.func @slotRewrite : (i256) -> i256 // CHECK: ^bb0(%[[ARG:.*]]: i256): // CHECK: %[[P:.*]] = llvm.alloca -// CHECK: %[[PP:.*]] = builtin.unrealized_conversion_cast %[[P]] : !llvm.ptr to !yul.ptr // CHECK: llvm.store %[[ARG]], %[[P]] // CHECK: %[[C3:.*]] = yul.constant 3 +// CHECK: %[[PP:.*]] = builtin.unrealized_conversion_cast %[[P]] : !llvm.ptr to !yul.ptr // CHECK: yul.store %[[C3]], %[[PP]] : i256, !yul.ptr // CHECK: %[[PV:.*]] = llvm.load %[[P]] // CHECK: yul.func_return %[[PV]] : i256