diff --git a/CMakeLists.txt b/CMakeLists.txt index 9e0490c3..4531052a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,6 +44,9 @@ include_directories(BEFORE include_directories("${LLVM_BINARY_DIR}/include" "${LLVM_INCLUDE_DIR}") link_directories("${LLVM_LIBRARY_DIR}") +set(THREADS_PREFER_PTHREAD_FLAG ON) +find_package(Threads REQUIRED) + set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_EXTENSIONS OFF) if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") @@ -57,6 +60,12 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") ) endif() +if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + add_compile_options( + -Wno-redundant-move + ) +endif() + option(SOLL_INCLUDE_TESTS "Generate build targets for the Soll unit tests." ${LLVM_INCLUDE_TESTS}) diff --git a/include/soll/AST/ASTContext.h b/include/soll/AST/ASTContext.h index 1589a161..4155f657 100644 --- a/include/soll/AST/ASTContext.h +++ b/include/soll/AST/ASTContext.h @@ -4,8 +4,8 @@ #include "soll/Frontend/FrontendOptions.h" #include #include +#include #include - namespace soll { class ASTContext : public llvm::RefCountedBase { diff --git a/include/soll/AST/Decl.h b/include/soll/AST/Decl.h index 5554e8d9..ffdeb49a 100644 --- a/include/soll/AST/Decl.h +++ b/include/soll/AST/Decl.h @@ -29,8 +29,7 @@ class Decl { friend class ASTReader; protected: - Decl(SourceRange L, - llvm::StringRef Name = llvm::StringRef::withNullAsEmpty(nullptr), + Decl(SourceRange L, llvm::StringRef Name = {}, Visibility vis = Visibility::Default) : Location(L), Name(Name.str()), Vis(vis), UniqueName(Name.str()) {} diff --git a/include/soll/Basic/DiagnosticSemaKinds.inc b/include/soll/Basic/DiagnosticSemaKinds.inc index 81740a68..266f61d7 100644 --- a/include/soll/Basic/DiagnosticSemaKinds.inc +++ b/include/soll/Basic/DiagnosticSemaKinds.inc @@ -33,3 +33,4 @@ DIAG(err_visibility_not_match, CLASS_ERROR, (unsigned)diag::Severity::Error, "vi DIAG(err_private_function_can_not_be_virtual, CLASS_ERROR, (unsigned)diag::Severity::Error, "private funtcion %0 can not be virtual", 0, false, 1) DIAG(err_mutability_overriding_not_allow, CLASS_ERROR, (unsigned)diag::Severity::Error, "can not change function %0 mutability", 0, false, 1) DIAG(err_func_to_var_overrided_unsoupport, CLASS_ERROR, (unsigned)diag::Severity::Error, "function to state variables override is unsupport", 0, false, 1) +DIAG(err_yul_access_special_object, CLASS_ERROR, (unsigned)diag::Severity::Error, "'%0' can not be access by 'datasize', 'dataoffset' or 'datacopy'.", 0, false, 1) diff --git a/include/soll/Basic/FileManager.h b/include/soll/Basic/FileManager.h index 661eadc6..15068128 100644 --- a/include/soll/Basic/FileManager.h +++ b/include/soll/Basic/FileManager.h @@ -2,6 +2,8 @@ #pragma once #include "soll/Basic/FileSystemOptions.h" #include +#include +#include #include #include #include diff --git a/include/soll/Basic/IdentifierTable.h b/include/soll/Basic/IdentifierTable.h index d86f3f00..5ccbecf6 100644 --- a/include/soll/Basic/IdentifierTable.h +++ b/include/soll/Basic/IdentifierTable.h @@ -3,6 +3,7 @@ #include "soll/Basic/TokenKinds.h" #include #include +#include namespace soll { diff --git a/lib/CodeGen/ABICodec.cpp b/lib/CodeGen/ABICodec.cpp index 17dff5b4..7f8bce7c 100644 --- a/lib/CodeGen/ABICodec.cpp +++ b/lib/CodeGen/ABICodec.cpp @@ -49,7 +49,8 @@ llvm::Value *AbiEmitter::emitEncodeTuple( std::tie(Value, IsStateVariable) = Values[I]; if (Value->getType()->isDynamic()) { DynamicPos.emplace_back(Int8Ptr, I); - Int8Ptr = Builder.CreateInBoundsGEP(Int8Ptr, {Builder.getIntN(32, 32)}); + Int8Ptr = Builder.CreateInBoundsGEP(CGM.Int8Ty, Int8Ptr, + {Builder.getIntN(32, 32)}); } else { Int8Ptr = emitEncode(Int8Ptr, Value, IsStateVariable); } @@ -96,7 +97,7 @@ AbiEmitter::getDecode(llvm::Value *Int8Ptr, const Type *Ty) { Builder.getIntN(32, 32)), Builder.getIntN(32, 32)); llvm::Value *NextInt8Ptr = - Builder.CreateInBoundsGEP(Int8Ptr, {EncodeLength}); + Builder.CreateInBoundsGEP(CGM.Int8Ty, Int8Ptr, {EncodeLength}); return {ExprValue::getRValue(Ty, Val), NextInt8Ptr}; } case Type::Category::Array: { @@ -133,19 +134,20 @@ AbiEmitter::getDecode(llvm::Value *Int8Ptr, const Type *Ty) { case Type::Category::FixedBytes: { llvm::Value *ValPtr = Builder.CreatePointerCast( Int8Ptr, llvm::PointerType::getUnqual(ValueTy)); - llvm::Value *Val = Builder.CreateLoad(ValPtr, ValueTy); + llvm::Value *Val = Builder.CreateLoad(ValueTy, ValPtr); llvm::Value *NextInt8Ptr = - Builder.CreateInBoundsGEP(Int8Ptr, {Builder.getInt32(32)}); + Builder.CreateInBoundsGEP(CGM.Int8Ty, Int8Ptr, {Builder.getInt32(32)}); return {ExprValue::getRValue(Ty, Val), NextInt8Ptr}; } default: { llvm::Value *ValPtr = Builder.CreatePointerCast( Int8Ptr, llvm::PointerType::getUnqual(CGM.Int256Ty)); - llvm::Value *Val = Builder.CreateLoad(ValPtr, CGM.Int256Ty); + llvm::Value *Val = Builder.CreateLoad(CGM.Int256Ty, ValPtr); Val = CGM.getEndianlessValue(Val); Val = Builder.CreateZExtOrTrunc(Val, ValueTy); llvm::Value *NextInt8Ptr = Builder.CreateInBoundsGEP( - Int8Ptr, {Builder.getInt32(ValueTy->getIntegerBitWidth() / 8)}); + CGM.Int8Ty, Int8Ptr, + {Builder.getInt32(ValueTy->getIntegerBitWidth() / 8)}); return {ExprValue::getRValue(Ty, Val), NextInt8Ptr}; } } @@ -162,7 +164,8 @@ AbiEmitter::getDecodeTuple(llvm::Value *Int8Ptr, const TupleType *Ty) { ExprValuePtr PosExprValue; std::tie(PosExprValue, NextInt8Ptr) = getDecode(NextInt8Ptr, &Int32Ty); llvm::Value *Pos = PosExprValue->load(Builder, CGM); - DynamicPos.emplace_back(Builder.CreateInBoundsGEP(Int8Ptr, {Pos}), I); + DynamicPos.emplace_back( + Builder.CreateInBoundsGEP(CGM.Int8Ty, Int8Ptr, {Pos}), I); } else { ExprValuePtr ValExprValue; std::tie(ValExprValue, NextInt8Ptr) = @@ -464,7 +467,7 @@ llvm::Value *AbiEmitter::emitEncodePacked(llvm::Value *Int8Ptr, unsigned PadRightLength = 32 - FixedBytesTy->getBitNum() / 8; if (PadRightLength % 32) Int8Ptr = Builder.CreateInBoundsGEP( - Int8Ptr, {Builder.getIntN(32, PadRightLength)}); + CGM.Int8Ty, Int8Ptr, {Builder.getIntN(32, PadRightLength)}); } return Int8Ptr; } @@ -502,7 +505,7 @@ llvm::Value *AbiEmitter::emitEncode(llvm::Value *Int8Ptr, Int8Ptr = copyToInt8Ptr( Int8Ptr, Builder.CreateZExtOrTrunc(Length, CGF.Int256Ty), true); Int8Ptr = copyToInt8Ptr(Int8Ptr, Bytes, true); - return Builder.CreateInBoundsGEP(Int8Ptr, {PadRightLength}); + return Builder.CreateInBoundsGEP(CGM.Int8Ty, Int8Ptr, {PadRightLength}); } case Type::Category::Array: { llvm::Function *ThisFunc = Builder.GetInsertBlock()->getParent(); @@ -530,7 +533,8 @@ llvm::Value *AbiEmitter::emitEncode(llvm::Value *Int8Ptr, if (ArrTy->getElementType()->isDynamic()) { Head = Int8Ptr; Tail = Builder.CreateInBoundsGEP( - Head, {Builder.CreateMul(ArrayLength, ABIStaticSizeValue)}); + CGM.Int8Ty, Head, + {Builder.CreateMul(ArrayLength, ABIStaticSizeValue)}); } else { Tail = Int8Ptr; } @@ -606,7 +610,7 @@ llvm::Value *AbiEmitter::emitEncode(llvm::Value *Int8Ptr, unsigned PadRightLength = 32 - FixedBytesTy->getBitNum() / 8; if (PadRightLength % 32) Int8Ptr = Builder.CreateInBoundsGEP( - Int8Ptr, {Builder.getIntN(32, PadRightLength)}); + CGM.Int8Ty, Int8Ptr, {Builder.getIntN(32, PadRightLength)}); return Int8Ptr; } default: { @@ -627,7 +631,7 @@ llvm::Value *AbiEmitter::copyToInt8Ptr(llvm::Value *Int8Ptr, llvm::Value *Value, Builder.CreateCall(Memcpy, {Builder.CreateBitCast(Int8Ptr, CGM.Int8PtrTy), SrcBytes, Length}); if (IncreasePtr) - return Builder.CreateInBoundsGEP(Int8Ptr, {Length}); + return Builder.CreateInBoundsGEP(CGM.Int8Ty, Int8Ptr, {Length}); } else { Value = CGM.getEndianlessValue(Value); llvm::Value *CPtr = @@ -635,7 +639,8 @@ llvm::Value *AbiEmitter::copyToInt8Ptr(llvm::Value *Int8Ptr, llvm::Value *Value, Builder.CreateStore(Value, CPtr); if (IncreasePtr) return Builder.CreateInBoundsGEP( - Int8Ptr, {Builder.getInt32(Ty->getIntegerBitWidth() / 8)}); + CGM.Int8Ty, Int8Ptr, + {Builder.getInt32(Ty->getIntegerBitWidth() / 8)}); } return nullptr; } diff --git a/lib/CodeGen/BackendUtil.cpp b/lib/CodeGen/BackendUtil.cpp index b70bd632..e401e119 100644 --- a/lib/CodeGen/BackendUtil.cpp +++ b/lib/CodeGen/BackendUtil.cpp @@ -6,6 +6,7 @@ #include "soll/Basic/TargetOptions.h" #include "soll/CodeGen/LoweringInteger.h" #include +#include #include #include #include @@ -142,16 +143,16 @@ void EmitAssemblyHelper::EmitAssembly( TheModule->setDataLayout(TM->createDataLayout()); } -#if LLVM_VERSION_MAJOR >= 9 - llvm::PassBuilder PB(TM.get(), llvm::PipelineTuningOptions(), llvm::None); +#if LLVM_VERSION_MAJOR == 12 + llvm::PassBuilder PB(false, TM.get()); #else - llvm::PassBuilder PB(TM.get(), llvm::None); + llvm::PassBuilder PB(TM.get()); #endif - llvm::LoopAnalysisManager LAM(false); - llvm::FunctionAnalysisManager FAM(false); - llvm::CGSCCAnalysisManager CGAM(false); - llvm::ModuleAnalysisManager MAM(false); + llvm::LoopAnalysisManager LAM; + llvm::FunctionAnalysisManager FAM; + llvm::CGSCCAnalysisManager CGAM; + llvm::ModuleAnalysisManager MAM; // Register the AA manager first so that our version is the one used. FAM.registerPass([&] { return PB.buildDefaultAAPipeline(); }); @@ -173,7 +174,7 @@ void EmitAssemblyHelper::EmitAssembly( PB.registerLoopAnalyses(LAM); PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); - llvm::ModulePassManager MPM(false); + llvm::ModulePassManager MPM; if (TargetOpts.BackendTarget == EWASM) { MPM.addPass(LoweringInteger()); @@ -183,19 +184,24 @@ void EmitAssemblyHelper::EmitAssembly( default: break; case O1: - MPM.addPass(PB.buildPerModuleDefaultPipeline(llvm::PassBuilder::O1)); + MPM.addPass(PB.buildPerModuleDefaultPipeline( + llvm::PassBuilder::OptimizationLevel::O1)); break; case O2: - MPM.addPass(PB.buildPerModuleDefaultPipeline(llvm::PassBuilder::O2)); + MPM.addPass(PB.buildPerModuleDefaultPipeline( + llvm::PassBuilder::OptimizationLevel::O2)); break; case O3: - MPM.addPass(PB.buildPerModuleDefaultPipeline(llvm::PassBuilder::O3)); + MPM.addPass(PB.buildPerModuleDefaultPipeline( + llvm::PassBuilder::OptimizationLevel::O3)); break; case Os: - MPM.addPass(PB.buildPerModuleDefaultPipeline(llvm::PassBuilder::Os)); + MPM.addPass(PB.buildPerModuleDefaultPipeline( + llvm::PassBuilder::OptimizationLevel::Os)); break; case Oz: - MPM.addPass(PB.buildPerModuleDefaultPipeline(llvm::PassBuilder::Oz)); + MPM.addPass(PB.buildPerModuleDefaultPipeline( + llvm::PassBuilder::OptimizationLevel::Oz)); break; } MPM.addPass(llvm::AlwaysInlinerPass()); diff --git a/lib/CodeGen/CGExpr.cpp b/lib/CodeGen/CGExpr.cpp index 8e90d1e9..eef8f864 100644 --- a/lib/CodeGen/CGExpr.cpp +++ b/lib/CodeGen/CGExpr.cpp @@ -10,6 +10,7 @@ #include #include #include + namespace soll::CodeGen { ExprValuePtr CodeGenFunction::emitExpr(const Expr *E) { @@ -381,7 +382,7 @@ ExprValuePtr CodeGenFunction::emitAbiDecode(const CallExpr *CE) { ExprValuePtr CodeGenFunction::emitAbiDecode(llvm::Value *Bytes, const Type *TupleTy) { - llvm::Value *Length = Builder.CreateZExtOrTrunc( + [[maybe_unused]] llvm::Value *Length = Builder.CreateZExtOrTrunc( Builder.CreateExtractValue(Bytes, {0}), CGM.Int32Ty); llvm::Value *SrcBytes = Builder.CreateExtractValue(Bytes, {1}); @@ -634,10 +635,11 @@ llvm::Value *CodeGenFunction::emitAsmGetBalance(const CallExpr *CE, llvm::Value *CodeGenFunction::emitAsmCallMLoad(const CallExpr *CE) { auto Arguments = CE->getArguments(); llvm::Value *Pos = emitExpr(Arguments[0])->load(Builder, CGM); - llvm::Value *CPtr = - Builder.CreateInBoundsGEP(CGM.getHeapBase(), {Pos}, "heap.cptr"); + llvm::Value *CPtr = Builder.CreateInBoundsGEP(CGM.Int8Ty, CGM.getHeapBase(), + {Pos}, "heap.cptr"); llvm::Value *Ptr = Builder.CreateBitCast(CPtr, Int256PtrTy, "heap.ptr"); - llvm::Value *Value = CGM.getEndianlessValue(Builder.CreateLoad(Ptr)); + llvm::Value *Value = + CGM.getEndianlessValue(Builder.CreateLoad(CGM.Int256Ty, Ptr)); CGM.emitUpdateMemorySize(Pos, Builder.getIntN(256, 32)); return Value; } @@ -645,8 +647,8 @@ llvm::Value *CodeGenFunction::emitAsmCallMLoad(const CallExpr *CE) { void CodeGenFunction::emitAsmCallMStore(const CallExpr *CE) { auto Arguments = CE->getArguments(); llvm::Value *Pos = emitExpr(Arguments[0])->load(Builder, CGM); - llvm::Value *CPtr = - Builder.CreateInBoundsGEP(CGM.getHeapBase(), {Pos}, "heap.cptr"); + llvm::Value *CPtr = Builder.CreateInBoundsGEP(CGM.Int8Ty, CGM.getHeapBase(), + {Pos}, "heap.cptr"); llvm::Value *Ptr = Builder.CreateBitCast(CPtr, Int256PtrTy, "heap.ptr"); Builder.CreateStore( CGM.getEndianlessValue(emitExpr(Arguments[1])->load(Builder, CGM)), Ptr); @@ -656,8 +658,8 @@ void CodeGenFunction::emitAsmCallMStore(const CallExpr *CE) { void CodeGenFunction::emitAsmCallMStore8(const CallExpr *CE) { auto Arguments = CE->getArguments(); llvm::Value *Pos = emitExpr(Arguments[0])->load(Builder, CGM); - llvm::Value *Ptr = - Builder.CreateInBoundsGEP(CGM.getHeapBase(), {Pos}, "heap.ptr"); + llvm::Value *Ptr = Builder.CreateInBoundsGEP(CGM.Int8Ty, CGM.getHeapBase(), + {Pos}, "heap.ptr"); llvm::Value *Value = Builder.CreateZExtOrTrunc( emitExpr(Arguments[1])->load(Builder, CGM), CGM.Int8Ty); Builder.CreateStore(Value, Ptr); @@ -665,7 +667,7 @@ void CodeGenFunction::emitAsmCallMStore8(const CallExpr *CE) { } llvm::Value *CodeGenFunction::emitAsmCallMSize(const CallExpr *CE) { - return Builder.CreateLoad(CGM.getMemorySize()); + return Builder.CreateLoad(CGM.Int256Ty, CGM.getMemorySize()); } llvm::Value *CodeGenFunction::emitAsmCallSLoad(const CallExpr *CE) { @@ -688,8 +690,8 @@ void CodeGenFunction::emitAsmCallSStore(const CallExpr *CE) { void CodeGenFunction::emitAsmCallReturn(const CallExpr *CE) { auto Arguments = CE->getArguments(); llvm::Value *Pos = emitExpr(Arguments[0])->load(Builder, CGM); - llvm::Value *Ptr = - Builder.CreateInBoundsGEP(CGM.getHeapBase(), {Pos}, "heap.cptr"); + llvm::Value *Ptr = Builder.CreateInBoundsGEP(CGM.Int8Ty, CGM.getHeapBase(), + {Pos}, "heap.cptr"); llvm::Value *Length = emitExpr(Arguments[1])->load(Builder, CGM); CGM.emitUpdateMemorySize(Pos, Length); CGM.emitFinish(Ptr, Builder.CreateZExtOrTrunc(Length, CGM.Int32Ty)); @@ -698,8 +700,8 @@ void CodeGenFunction::emitAsmCallReturn(const CallExpr *CE) { void CodeGenFunction::emitAsmCallRevert(const CallExpr *CE) { auto Arguments = CE->getArguments(); llvm::Value *Pos = emitExpr(Arguments[0])->load(Builder, CGM); - llvm::Value *Ptr = - Builder.CreateInBoundsGEP(CGM.getHeapBase(), {Pos}, "heap.cptr"); + llvm::Value *Ptr = Builder.CreateInBoundsGEP(CGM.Int8Ty, CGM.getHeapBase(), + {Pos}, "heap.cptr"); llvm::Value *Length = emitExpr(Arguments[1])->load(Builder, CGM); CGM.emitUpdateMemorySize(Pos, Length); CGM.emitRevert(Ptr, Builder.CreateZExtOrTrunc(Length, CGM.Int32Ty)); @@ -714,8 +716,8 @@ void CodeGenFunction::emitAsmSelfDestruct(const CallExpr *CE) { void CodeGenFunction::emitAsmCallLog(const CallExpr *CE) { auto Arguments = CE->getArguments(); llvm::Value *Pos = emitExpr(Arguments[0])->load(Builder, CGM); - llvm::Value *Data = - Builder.CreateInBoundsGEP(CGM.getHeapBase(), {Pos}, "heap.cptr"); + llvm::Value *Data = Builder.CreateInBoundsGEP(CGM.Int8Ty, CGM.getHeapBase(), + {Pos}, "heap.cptr"); llvm::Value *DataLength = emitExpr(Arguments[1])->load(Builder, CGM); std::vector Topics; for (size_t I = 2; I < Arguments.size(); I++) { @@ -739,8 +741,8 @@ llvm::Value *CodeGenFunction::emitAsmCallCallDataLoad(const CallExpr *CE) { void CodeGenFunction::emitAsmCallCodeCopy(const CallExpr *CE) { auto Arguments = CE->getArguments(); llvm::Value *Pos = emitExpr(Arguments[0])->load(Builder, CGM); - llvm::Value *Ptr = - Builder.CreateInBoundsGEP(CGM.getHeapBase(), {Pos}, "heap.ptr"); + llvm::Value *Ptr = Builder.CreateInBoundsGEP(CGM.Int8Ty, CGM.getHeapBase(), + {Pos}, "heap.ptr"); llvm::Value *Code = Builder.CreateIntToPtr( emitExpr(Arguments[1])->load(Builder, CGM), Int8PtrTy); llvm::Value *Length = emitExpr(Arguments[2])->load(Builder, CGM); @@ -802,8 +804,8 @@ llvm::Value *CodeGenFunction::emitAsmCallkeccak256(const CallExpr *CE) { // keccak256 function auto Arguments = CE->getArguments(); llvm::Value *Pos = emitExpr(Arguments[0])->load(Builder, CGM); - llvm::Value *Ptr = - Builder.CreateInBoundsGEP(CGM.getHeapBase(), {Pos}, "heap.cptr"); + llvm::Value *Ptr = Builder.CreateInBoundsGEP(CGM.Int8Ty, CGM.getHeapBase(), + {Pos}, "heap.cptr"); llvm::Value *Length = emitExpr(Arguments[1])->load(Builder, CGM); CGM.emitUpdateMemorySize(Pos, Length); return CGM.emitKeccak256(Ptr, Length); @@ -960,7 +962,7 @@ llvm::Value *CodeGenFunction::emitAsmCreate(const CallExpr *CE) { llvm::Value *AddressPtr = Builder.CreateAlloca(AddressTy); CGM.emitCreate(ValuePtr, Ptr, Length, AddressPtr); - return Builder.CreateLoad(AddressPtr, AddressTy); + return Builder.CreateLoad(AddressTy, AddressPtr); } llvm::Value *CodeGenFunction::emitAsmCreate2(const CallExpr *CE) { @@ -977,7 +979,7 @@ llvm::Value *CodeGenFunction::emitAsmCreate2(const CallExpr *CE) { llvm::Value *AddressPtr = Builder.CreateAlloca(AddressTy); CGM.emitCreate2(ValuePtr, Ptr, Length, Salt, AddressPtr); - return Builder.CreateLoad(AddressPtr, AddressTy); + return Builder.CreateLoad(AddressTy, AddressPtr); } llvm::Value *CodeGenFunction::emitAsmByte(const CallExpr *CE) { @@ -993,7 +995,7 @@ llvm::Value *CodeGenFunction::emitAsmByte(const CallExpr *CE) { llvm::Value *CodeGenFunction::emitAsmChainId(const CallExpr *CE) { llvm::Value *ValPtr = Builder.CreateAlloca(Int128Ty, nullptr); CGM.emitGetChainId(ValPtr); - return Builder.CreateLoad(ValPtr, Int128Ty); + return Builder.CreateLoad(Int128Ty, ValPtr); } llvm::Value *CodeGenFunction::emitAsmLinkersymbol(const CallExpr *CE) { @@ -1014,14 +1016,15 @@ void CodeGenFunction::emitAsmSetImmutable(const CallExpr *CE) { if (Map.find(StringRefName) != Map.end()) { llvm::Value *Offset = emitExpr(Arguments[0])->load(Builder, CGM); llvm::Value *TableOffset = Builder.getInt(Map.lookup(StringRefName)); - llvm::Value *ImmutableCPtr = Builder.CreateInBoundsGEP( - CGM.getImmutableBase(), {TableOffset}, "immutable.cptr"); + llvm::Value *ImmutableCPtr = + Builder.CreateInBoundsGEP(CGM.Int256Ty, CGM.getImmutableBase(), + {TableOffset}, "immutable.cptr"); Builder.CreateStore( Offset, Builder.CreateBitCast(ImmutableCPtr, Int256PtrTy, "immutable.ptr")); - llvm::Value *HeapCPtr = - Builder.CreateInBoundsGEP(CGM.getHeapBase(), {Offset}, "heap.cptr"); + llvm::Value *HeapCPtr = Builder.CreateInBoundsGEP( + CGM.Int8Ty, CGM.getHeapBase(), {Offset}, "heap.cptr"); Builder.CreateStore( Value, Builder.CreateBitCast(HeapCPtr, Int256PtrTy, "heap.ptr")); CGM.emitUpdateMemorySize(Offset, Builder.getIntN(256, 0x20)); @@ -1043,14 +1046,17 @@ llvm::Value *CodeGenFunction::emitAsmLoadImmutable(const CallExpr *CE) { auto &Map = Ctx.getImmutableAddressMap(); if (Map.find(StringRefName) != Map.end()) { llvm::Value *TableOffset = Builder.getInt(Map.lookup(StringRefName)); - llvm::Value *ImmutableCPtr = Builder.CreateInBoundsGEP( - CGM.getImmutableBase(), {TableOffset}, "immutable.cptr"); + llvm::Value *ImmutableCPtr = + Builder.CreateInBoundsGEP(CGM.Int256Ty, CGM.getImmutableBase(), + {TableOffset}, "immutable.cptr"); llvm::Value *Offset = Builder.CreateLoad( + CGM.Int256Ty, Builder.CreateBitCast(ImmutableCPtr, Int256PtrTy, "immutable.ptr")); - llvm::Value *HeapCPtr = - Builder.CreateInBoundsGEP(CGM.getHeapBase(), {Offset}, "heap.cptr"); + llvm::Value *HeapCPtr = Builder.CreateInBoundsGEP( + CGM.Int8Ty, CGM.getHeapBase(), {Offset}, "heap.cptr"); return Builder.CreateLoad( + CGM.Int256Ty, Builder.CreateBitCast(HeapCPtr, Int256PtrTy, "heap.ptr")); } } diff --git a/lib/CodeGen/CGValue.h b/lib/CodeGen/CGValue.h index 8a1f22b5..40739f52 100644 --- a/lib/CodeGen/CGValue.h +++ b/lib/CodeGen/CGValue.h @@ -50,9 +50,10 @@ class ExprValue { case ValueKind::VK_RValue: return V; case ValueKind::VK_LValue: - return Builder.CreateLoad(V, Name); + return Builder.CreateLoad(V->getType()->getPointerElementType(), V, Name); case ValueKind::VK_SValue: - llvm::Value *Address = CGM.getEndianlessValue(Builder.CreateLoad(V)); + llvm::Value *Address = CGM.getEndianlessValue( + Builder.CreateLoad(V->getType()->getPointerElementType(), V)); llvm::Value *Val = CGM.emitStorageLoad(Address); switch (Ty->getCategory()) { case Type::Category::Address: @@ -153,8 +154,8 @@ class ExprValue { Builder.CreateStore(CGM.getEndianlessValue(PHIAddress), PHIAddressPtr); if (CGM.isEVM()) { llvm::Value *LoadV = Builder.CreateCall( - StorageLoad, - {CGM.getEndianlessValue(Builder.CreateLoad(PHIAddressPtr))}); + StorageLoad, {CGM.getEndianlessValue(Builder.CreateLoad( + CGM.Int256Ty, PHIAddressPtr))}); Builder.CreateStore(CGM.getEndianlessValue(LoadV), Builder.CreateBitCast(PHIPtr, CGM.Int256PtrTy)); } else if (CGM.isEWASM()) { @@ -166,8 +167,8 @@ class ExprValue { Builder.CreateSub(PHIRemain, Builder.getIntN(256, 32)); llvm::Value *NextAddress = Builder.CreateAdd(PHIAddress, Builder.getIntN(256, 1)); - llvm::Value *NextendPtr = - Builder.CreateInBoundsGEP(PHIPtr, {Builder.getInt32(32)}); + llvm::Value *NextendPtr = Builder.CreateInBoundsGEP( + CGM.Int8Ty, PHIPtr, {Builder.getInt32(32)}); Condition = Builder.CreateICmpSGE(NextRemain, Builder.getIntN(256, 32)); Builder.CreateCondBr(Condition, Loop, LoopEnd); PHIRemain->addIncoming(ExtendLength, ExtendSlot); @@ -198,8 +199,8 @@ class ExprValue { ValPtr = Builder.CreateAlloca(CGM.Int256Ty); if (CGM.isEVM()) { llvm::Value *LoadV = Builder.CreateCall( - StorageLoad, - {CGM.getEndianlessValue(Builder.CreateLoad(AddressPtr))}); + StorageLoad, {CGM.getEndianlessValue( + Builder.CreateLoad(CGM.Int256Ty, AddressPtr))}); Builder.CreateStore(CGM.getEndianlessValue(LoadV), ValPtr); } else if (CGM.isEWASM()) { Builder.CreateCall(StorageLoad, {AddressPtr, ValPtr}); @@ -252,7 +253,8 @@ class ExprValue { Builder.CreateStore(Value, V); return; case ValueKind::VK_SValue: - llvm::Value *Address = CGM.getEndianlessValue(Builder.CreateLoad(V)); + llvm::Value *Address = CGM.getEndianlessValue( + Builder.CreateLoad(V->getType()->getPointerElementType(), V)); switch (Ty->getCategory()) { case Type::Category::Address: case Type::Category::Bool: @@ -318,11 +320,11 @@ class ExprValue { CGM.emitMemcpy(Builder.CreateBitCast(ValPtr, CGM.BytesElemPtrTy), Builder.CreateBitCast(Ptr, CGM.BytesElemPtrTy), Builder.CreateZExtOrTrunc(Length, CGM.Int32Ty)); - llvm::Value *Val = Builder.CreateLoad(ValPtr); + llvm::Value *Val = Builder.CreateLoad(CGM.Int256Ty, ValPtr); Val = Builder.CreateOr(Val, CGM.getEndianlessValue(LengthEncode)); Builder.CreateStore(Val, ValPtr); - CGM.emitStorageStore(Builder.CreateLoad(AddressPtr), - Builder.CreateLoad(ValPtr)); + CGM.emitStorageStore(Builder.CreateLoad(CGM.Int256Ty, AddressPtr), + Builder.CreateLoad(CGM.Int256Ty, ValPtr)); Builder.CreateBr(Done); // ExtendSlot case @@ -342,8 +344,8 @@ class ExprValue { LengthEncode = CGM.getEndianlessValue( Builder.CreateOr(LengthEncode, Builder.getIntN(256, 1))); Builder.CreateStore(LengthEncode, ValPtr); - CGM.emitStorageStore(Builder.CreateLoad(AddressPtr), - Builder.CreateLoad(ValPtr)); + CGM.emitStorageStore(Builder.CreateLoad(CGM.Int256Ty, AddressPtr), + Builder.CreateLoad(CGM.Int256Ty, ValPtr)); llvm::Value *Bytes = CGM.emitConcatBytes({Address}); Address = CGM.emitKeccak256(Bytes); Condition = Builder.CreateICmpSGE(Length, Builder.getIntN(256, 32)); @@ -356,14 +358,16 @@ class ExprValue { llvm::PHINode *PHIAddressPtr = Builder.CreatePHI(CGM.Int256PtrTy, 2); Builder.CreateStore(CGM.getEndianlessValue(PHIAddress), PHIAddressPtr); CGM.emitStorageStore( - Builder.CreateLoad(PHIAddressPtr), - Builder.CreateLoad(Builder.CreateBitCast(PHIPtr, CGM.Int256PtrTy))); + Builder.CreateLoad(CGM.Int256Ty, PHIAddressPtr), + Builder.CreateLoad(CGM.Int256Ty, + Builder.CreateBitCast(PHIPtr, CGM.Int256PtrTy))); llvm::Value *NextRemain = Builder.CreateSub(PHIRemain, Builder.getIntN(256, 32)); llvm::Value *NextAddress = Builder.CreateAdd(PHIAddress, Builder.getIntN(256, 1)); llvm::Value *NextendPtr = Builder.CreateInBoundsGEP( - PHIPtr, {Builder.getInt32(0), Builder.getInt32(32)}); + PHIPtr->getType()->getPointerElementType(), PHIPtr, + {Builder.getInt32(0), Builder.getInt32(32)}); NextendPtr = Builder.CreateBitCast(NextendPtr, Array32Int8Ptr); Condition = Builder.CreateICmpSGE(NextRemain, Builder.getIntN(256, 32)); Builder.CreateCondBr(Condition, Loop, LoopEnd); @@ -396,8 +400,8 @@ class ExprValue { CGM.emitMemcpy(Builder.CreateBitCast(ValPtr, CGM.BytesElemPtrTy), Builder.CreateBitCast(PHIPtr, CGM.BytesElemPtrTy), Builder.CreateZExtOrTrunc(PHIRemain, CGM.Int32Ty)); - CGM.emitStorageStore(Builder.CreateLoad(AddressPtr), - Builder.CreateLoad(ValPtr)); + CGM.emitStorageStore(Builder.CreateLoad(CGM.Int256Ty, AddressPtr), + Builder.CreateLoad(CGM.Int256Ty, ValPtr)); Builder.CreateBr(Done); Builder.SetInsertPoint(Done); diff --git a/lib/CodeGen/CodeGenAction.cpp b/lib/CodeGen/CodeGenAction.cpp index 85476101..74e1aa92 100644 --- a/lib/CodeGen/CodeGenAction.cpp +++ b/lib/CodeGen/CodeGenAction.cpp @@ -7,6 +7,7 @@ #include "llvm/Support/Alignment.h" #include #include +#include #include #include #include @@ -131,7 +132,13 @@ class BackendConsumer : public ASTConsumer { llvm::BasicBlock *Entry = llvm::BasicBlock::Create(VMContext, "entry", Func); Builder.SetInsertPoint(Entry); - llvm::Value *Result = llvm::UndefValue::get(Module.getTypeByName("bytes")); + llvm::Value *Result = +#if LLVM_VERSION_MAJOR >= 12 + llvm::UndefValue::get( + llvm::StructType::getTypeByName(VMContext, "bytes")); +#else + llvm::UndefValue::get(Module.getTypeByName("bytes")); +#endif Result = Builder.CreateInsertValue(Result, Builder.getIntN(256, Length), 0); Result = Builder.CreateInsertValue( Result, Builder.CreateBitCast(GV, Builder.getInt8PtrTy()), 1); @@ -181,7 +188,7 @@ class BackendConsumer : public ASTConsumer { if (auto Error = removeExports(Wasm->TmpName)) { llvm::consumeError(Wasm->discard()); llvm::consumeError(Object->discard()); - return Error; + return std::move(Error); } auto Binary = llvm::MemoryBuffer::getFile(Wasm->TmpName); @@ -289,7 +296,7 @@ std::unique_ptr CodeGenAction::CreateASTConsumer(CompilerInstance &CI, llvm::StringRef InFile) { return std::make_unique( Action, CI.getDiagnostics(), CI.getCodeGenOpts(), CI.getTargetOpts(), - InFile, *VMContext, CI.GetOutputStreamFunc()); + InFile.str(), *VMContext, CI.GetOutputStreamFunc()); } EmitAssemblyAction::EmitAssemblyAction(llvm::LLVMContext *VMContext) diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp index 5fd464d1..5a22b344 100644 --- a/lib/CodeGen/CodeGenModule.cpp +++ b/lib/CodeGen/CodeGenModule.cpp @@ -155,7 +155,8 @@ void CodeGenModule::initUpdateMemorySize() { llvm::BasicBlock *Done = llvm::BasicBlock::Create(VMContext, "done", Func_updateMemorySize); Builder.SetInsertPoint(Entry); - llvm::Value *OrigSize = Builder.CreateLoad(MemorySize, "memory.size"); + llvm::Value *OrigSize = + Builder.CreateLoad(Int256Ty, MemorySize, "memory.size"); llvm::Value *EndPos = Builder.CreateAdd(Pos, Range); llvm::Value *Condition = Builder.CreateICmpUGT(EndPos, OrigSize); Builder.CreateCondBr(Condition, Update, Done); @@ -951,10 +952,10 @@ void CodeGenModule::initMemcpy() { llvm::PHINode *DstPHI = Builder.CreatePHI(BytesElemPtrTy, 2); llvm::PHINode *LengthPHI = Builder.CreatePHI(Int32Ty, 2); - llvm::Value *Value = Builder.CreateLoad(SrcPHI); + llvm::Value *Value = Builder.CreateLoad(BytesElemTy, SrcPHI); Builder.CreateStore(Value, DstPHI); - llvm::Value *Src2 = Builder.CreateInBoundsGEP(SrcPHI, {One}); - llvm::Value *Dst2 = Builder.CreateInBoundsGEP(DstPHI, {One}); + llvm::Value *Src2 = Builder.CreateInBoundsGEP(BytesElemTy, SrcPHI, {One}); + llvm::Value *Dst2 = Builder.CreateInBoundsGEP(BytesElemTy, DstPHI, {One}); llvm::Value *Length2 = Builder.CreateSub(LengthPHI, One); llvm::Value *Cmp2 = Builder.CreateICmpNE(Length2, Builder.getInt32(0)); Builder.CreateCondBr(Cmp2, Loop, Return); @@ -1036,7 +1037,7 @@ void CodeGenModule::initKeccak256() { Builder.CreateBitCast(ResultPtr, Int8PtrTy, "result.vptr"); Builder.CreateCall(Func_returnDataCopy, {ResultVPtr, Builder.getInt32(0), Builder.getInt32(32)}); - llvm::Value *Result = Builder.CreateLoad(ResultPtr); + llvm::Value *Result = Builder.CreateLoad(Int256Ty, ResultPtr); Builder.CreateRet(emitEndianConvert(Result)); } else { @@ -1075,7 +1076,7 @@ void CodeGenModule::initSha256() { Builder.CreateBitCast(ResultPtr, Int8PtrTy, "result.vptr"); Builder.CreateCall(Func_returnDataCopy, {ResultVPtr, Builder.getInt32(0), Builder.getInt32(32)}); - llvm::Value *Result = Builder.CreateLoad(ResultPtr); + llvm::Value *Result = Builder.CreateLoad(Int256Ty, ResultPtr); Builder.CreateRet(emitEndianConvert(Result)); } else { @@ -1105,7 +1106,7 @@ void CodeGenModule::initRipemd160() { emitCallStatic(Fee, AddressPtr, Ptr, Length, ResultPtr, Builder.getIntN(256, 0x20)); llvm::Value *Result = - Builder.CreateTrunc(Builder.CreateLoad(ResultPtr), Int160Ty); + Builder.CreateTrunc(Builder.CreateLoad(EVMIntTy, ResultPtr), Int160Ty); Builder.CreateRet(Result); } else if (isEWASM()) { llvm::Value *AddressPtr = @@ -1121,7 +1122,7 @@ void CodeGenModule::initRipemd160() { Builder.CreateBitCast(ResultPtr, Int8PtrTy, "result.vptr"); Builder.CreateCall(Func_returnDataCopy, {ResultVPtr, Builder.getInt32(0), Builder.getInt32(32)}); - llvm::Value *Result = Builder.CreateLoad(ResultPtr); + llvm::Value *Result = Builder.CreateLoad(Int256Ty, ResultPtr); Builder.CreateRet(Builder.CreateTrunc(emitEndianConvert(Result), Int160Ty)); } else { @@ -1169,7 +1170,7 @@ void CodeGenModule::initEcrecover() { emitCallStatic(Fee, AddressPtr, Ptr, Length, ResultPtr, Builder.getIntN(256, 0x20)); llvm::Value *Result = - Builder.CreateTrunc(Builder.CreateLoad(ResultPtr), AddressTy); + Builder.CreateTrunc(Builder.CreateLoad(EVMIntTy, ResultPtr), AddressTy); Builder.CreateRet(Result); } else if (isEWASM()) { llvm::Value *AddressPtr = @@ -1185,7 +1186,7 @@ void CodeGenModule::initEcrecover() { Builder.CreateBitCast(ResultPtr, Int8PtrTy, "result.vptr"); Builder.CreateCall(Func_returnDataCopy, {ResultVPtr, Builder.getInt32(0), Builder.getInt32(32)}); - llvm::Value *Result = Builder.CreateLoad(ResultPtr); + llvm::Value *Result = Builder.CreateLoad(Int256Ty, ResultPtr); Builder.CreateRet(Builder.CreateTrunc(emitEndianConvert(Result), Int160Ty)); } else { @@ -1373,7 +1374,8 @@ llvm::Value *CodeGenModule::emitABILoadParamStatic(const Type *Ty, LLVMTy = getLLVMType(Ty); } llvm::Value *CPtr = Builder.CreateInBoundsGEP( - Buffer, {Builder.getInt32(Offset)}, Name + ".cptr"); + Buffer->getType()->getPointerElementType(), Buffer, + {Builder.getInt32(Offset)}, Name + ".cptr"); llvm::Value *Ptr = Builder.CreateBitCast(CPtr, Int256PtrTy, Name + ".ptr"); llvm::Value *ValB = Builder.CreateLoad(Int256Ty, Ptr, Name + ".b"); llvm::Value *Val = getEndianlessValue(ValB); @@ -1405,14 +1407,16 @@ llvm::Value *CodeGenModule::emitABILoadParamDynamic(const Type *Ty, } } else if (dynamic_cast(Ty)) { llvm::Value *CPtr = - Builder.CreateInBoundsGEP(Buffer, {Offset}, Name + ".cptr"); + Builder.CreateInBoundsGEP(Buffer->getType()->getPointerElementType(), + Buffer, {Offset}, Name + ".cptr"); llvm::Value *String = llvm::UndefValue::get(StringTy); String = Builder.CreateInsertValue(String, Size, {0}); String = Builder.CreateInsertValue(String, CPtr, {1}); return String; } else if (dynamic_cast(Ty)) { llvm::Value *CPtr = - Builder.CreateInBoundsGEP(Buffer, {Offset}, Name + ".cptr"); + Builder.CreateInBoundsGEP(Buffer->getType()->getPointerElementType(), + Buffer, {Offset}, Name + ".cptr"); llvm::Value *Bytes = llvm::UndefValue::get(BytesTy); Bytes = Builder.CreateInsertValue(Bytes, Size, {0}); Bytes = Builder.CreateInsertValue(Bytes, CPtr, {1}); @@ -1605,7 +1609,7 @@ void CodeGenModule::emitStructDecl(const StructDecl *SD) { void CodeGenModule::emitYulObject(const YulObject *YO) { { - const std::string Name = YO->getUniqueName(); + const std::string Name = YO->getUniqueName().str(); emitNestedObjectGetter(Name + ".object"); NestedEntries.emplace_back(Name + ".main", Name + ".object", nullptr); /* @@ -1618,8 +1622,30 @@ void CodeGenModule::emitYulObject(const YulObject *YO) { */ } assert(nullptr != YO->getCode()); + if (YO->getName() == ".metadata") { + // ignore sub-object and code + for (const auto *D : YO->getDataList()) { + emitYulDataInMetadata(D); + } + return; + } for (const auto *O : YO->getObjectList()) { emitYulObject(O); + // FIXME + { + const std::string Name = O->getName().str(); + emitNestedObjectGetter(Name + ".object"); + NestedEntries.emplace_back(Name + ".main", Name + ".object", nullptr); + /* + llvm::Function *DataSize = llvm::Function::Create( + llvm::FunctionType::get(Int256Ty, false), + llvm::Function::InternalLinkage, ".datasize", TheModule); + llvm::Function *DataOffset = llvm::Function::Create( + llvm::FunctionType::get(Int8PtrTy, false), + llvm::Function::InternalLinkage, ".dataoffset", TheModule); + */ + // LookupYulDataOrYulObject.try_emplace(Name, O); + } getEntry().clear(); getEntry().resize(1); } @@ -1655,11 +1681,31 @@ void CodeGenModule::emitYulCode(const YulCode *YC, llvm::StringRef Name) { void CodeGenModule::emitYulData(const YulData *YD) { llvm::StringRef Name = YD->getUniqueName(); std::string Data = YD->getBody()->getValue(); + if (YD->getName() == ".metadata") { + llvm::SmallVector Ops; + llvm::StringRef Name = YD->getName(); + std::string Data = YD->getBody()->getValue(); + Ops.push_back(llvm::MDString::get(VMContext, Name)); + Ops.push_back(llvm::MDString::get(VMContext, Data)); + auto MD = getModule().getOrInsertNamedMetadata("wasm.custom_sections"); + MD->addOperand(llvm::MDTuple::get(VMContext, Ops)); + return; + } llvm::GlobalVariable *Variable = createGlobalString(VMContext, getModule(), Data, Name); YulDataMap.try_emplace(YD, Variable); } +void CodeGenModule::emitYulDataInMetadata(const YulData *YD) { + llvm::SmallVector Ops; + std::string Name = ".metadata." + YD->getName().str(); + std::string Data = YD->getBody()->getValue(); + Ops.push_back(llvm::MDString::get(VMContext, Name)); + Ops.push_back(llvm::MDString::get(VMContext, Data)); + auto MD = getModule().getOrInsertNamedMetadata("wasm.custom_sections"); + MD->addOperand(llvm::MDTuple::get(VMContext, Ops)); +} + std::string CodeGenModule::getMangledName(const CallableVarDecl *CVD) { // XXX: Implement mangling std::string Name = CVD->getUniqueName().str() + '('; @@ -1851,7 +1897,7 @@ CodeGenModule::emitConcatBytes(llvm::ArrayRef Values) { llvm::Value *Array = Builder.CreateAlloca(BytesElemTy, ArrayLength, "concat"); llvm::Value *Index = Builder.getInt32(0); for (llvm::Value *Value : Values) { - llvm::Value *Ptr = Builder.CreateInBoundsGEP(Array, {Index}); + llvm::Value *Ptr = Builder.CreateInBoundsGEP(BytesElemTy, Array, {Index}); llvm::Type *Ty = Value->getType(); if (isDynamicType(Ty)) { llvm::Value *Length = Builder.CreateZExtOrTrunc( @@ -1891,7 +1937,7 @@ llvm::Value *CodeGenModule::emitGetCallValue() { } else if (isEWASM()) { llvm::Value *ValPtr = Builder.CreateAlloca(Int128Ty); Builder.CreateCall(Func_getCallValue, {ValPtr}); - return Builder.CreateLoad(ValPtr); + return Builder.CreateLoad(Int128Ty, ValPtr); } else { __builtin_unreachable(); } @@ -1904,7 +1950,7 @@ llvm::Value *CodeGenModule::emitGetCaller() { } else if (isEWASM()) { llvm::Value *ValPtr = Builder.CreateAlloca(AddressTy); Builder.CreateCall(Func_getCaller, {ValPtr}); - return Builder.CreateLoad(ValPtr); + return Builder.CreateLoad(AddressTy, ValPtr); } else { __builtin_unreachable(); } @@ -1951,7 +1997,9 @@ void CodeGenModule::emitLog(llvm::Value *DataOffset, llvm::Value *DataLength, {Builder.CreatePtrToInt( Builder.CreateBitCast(DataOffset, Builder.getInt8PtrTy()), EVMIntTy), - Length, Builder.CreateLoad(Topics[0])}); + Length, + Builder.CreateLoad(Topics[0]->getType()->getPointerElementType(), + Topics[0])}); break; case 2: Builder.CreateCall( @@ -1959,8 +2007,11 @@ void CodeGenModule::emitLog(llvm::Value *DataOffset, llvm::Value *DataLength, {Builder.CreatePtrToInt( Builder.CreateBitCast(DataOffset, Builder.getInt8PtrTy()), EVMIntTy), - Length, Builder.CreateLoad(Topics[0]), - Builder.CreateLoad(Topics[1])}); + Length, + Builder.CreateLoad(Topics[0]->getType()->getPointerElementType(), + Topics[0]), + Builder.CreateLoad(Topics[1]->getType()->getPointerElementType(), + Topics[1])}); break; case 3: Builder.CreateCall( @@ -1968,8 +2019,13 @@ void CodeGenModule::emitLog(llvm::Value *DataOffset, llvm::Value *DataLength, {Builder.CreatePtrToInt( Builder.CreateBitCast(DataOffset, Builder.getInt8PtrTy()), EVMIntTy), - Length, Builder.CreateLoad(Topics[0]), Builder.CreateLoad(Topics[1]), - Builder.CreateLoad(Topics[2])}); + Length, + Builder.CreateLoad(Topics[0]->getType()->getPointerElementType(), + Topics[0]), + Builder.CreateLoad(Topics[1]->getType()->getPointerElementType(), + Topics[1]), + Builder.CreateLoad(Topics[2]->getType()->getPointerElementType(), + Topics[2])}); break; case 4: Builder.CreateCall( @@ -1977,8 +2033,15 @@ void CodeGenModule::emitLog(llvm::Value *DataOffset, llvm::Value *DataLength, {Builder.CreatePtrToInt( Builder.CreateBitCast(DataOffset, Builder.getInt8PtrTy()), EVMIntTy), - Length, Builder.CreateLoad(Topics[0]), Builder.CreateLoad(Topics[1]), - Builder.CreateLoad(Topics[2]), Builder.CreateLoad(Topics[3])}); + Length, + Builder.CreateLoad(Topics[0]->getType()->getPointerElementType(), + Topics[0]), + Builder.CreateLoad(Topics[1]->getType()->getPointerElementType(), + Topics[1]), + Builder.CreateLoad(Topics[2]->getType()->getPointerElementType(), + Topics[2]), + Builder.CreateLoad(Topics[3]->getType()->getPointerElementType(), + Topics[3])}); break; default: __builtin_unreachable(); @@ -2019,7 +2082,7 @@ llvm::Value *CodeGenModule::emitStorageLoad(llvm::Value *Address) { llvm::Value *ValPtr = Builder.CreateAlloca(Int256Ty, nullptr); Builder.CreateStore(Address, AddressPtr); Builder.CreateCall(Func_storageLoad, {AddressPtr, ValPtr}); - return Builder.CreateLoad(ValPtr); + return Builder.CreateLoad(Int256Ty, ValPtr); } else { __builtin_unreachable(); } @@ -2352,7 +2415,7 @@ llvm::Value *CodeGenModule::emitGetTxGasPrice() { } else if (isEWASM()) { llvm::Value *ValPtr = Builder.CreateAlloca(Int128Ty); Builder.CreateCall(Func_getTxGasPrice, {ValPtr}); - return Builder.CreateLoad(ValPtr); + return Builder.CreateLoad(Int128Ty, ValPtr); } else { __builtin_unreachable(); } @@ -2365,7 +2428,7 @@ llvm::Value *CodeGenModule::emitGetTxOrigin() { } else if (isEWASM()) { llvm::Value *ValPtr = Builder.CreateAlloca(AddressTy); Builder.CreateCall(Func_getTxOrigin, {ValPtr}); - return Builder.CreateLoad(ValPtr); + return Builder.CreateLoad(AddressTy, ValPtr); } else { __builtin_unreachable(); } @@ -2378,7 +2441,7 @@ llvm::Value *CodeGenModule::emitGetBlockCoinbase() { } else if (isEWASM()) { llvm::Value *ValPtr = Builder.CreateAlloca(AddressTy); Builder.CreateCall(Func_getBlockCoinbase, {ValPtr}); - return Builder.CreateLoad(ValPtr); + return Builder.CreateLoad(AddressTy, ValPtr); } else { __builtin_unreachable(); } @@ -2390,7 +2453,7 @@ llvm::Value *CodeGenModule::emitGetBlockDifficulty() { } else if (isEWASM()) { llvm::Value *ValPtr = Builder.CreateAlloca(Int256Ty); Builder.CreateCall(Func_getBlockDifficulty, {ValPtr}); - return Builder.CreateLoad(ValPtr); + return Builder.CreateLoad(Int256Ty, ValPtr); } else { __builtin_unreachable(); } @@ -2431,7 +2494,7 @@ llvm::Value *CodeGenModule::emitGetBlockHash(llvm::Value *Number) { llvm::Value *ValPtr = Builder.CreateAlloca(Int256Ty); Builder.CreateCall(Func_getBlockHash, {Builder.CreateZExtOrTrunc(Number, Int64Ty), ValPtr}); - return Builder.CreateLoad(ValPtr); + return Builder.CreateLoad(Int256Ty, ValPtr); } else { __builtin_unreachable(); } @@ -2449,7 +2512,7 @@ llvm::Value *CodeGenModule::emitGetExternalBalance(llvm::Value *Address) { Builder.CreateCall(Func_getExternalBalance, {Builder.CreateBitCast(AddressPtr, Int32PtrTy), Builder.CreateBitCast(ValPtr, Int32PtrTy)}); - return Builder.CreateLoad(ValPtr); + return Builder.CreateLoad(Int128Ty, ValPtr); } else { __builtin_unreachable(); } @@ -2462,7 +2525,7 @@ llvm::Value *CodeGenModule::emitGetAddress() { } else if (isEWASM()) { llvm::Value *ValPtr = Builder.CreateAlloca(AddressTy); Builder.CreateCall(Func_getAddress, {ValPtr}); - return Builder.CreateLoad(ValPtr); + return Builder.CreateLoad(AddressTy, ValPtr); } else { __builtin_unreachable(); } diff --git a/lib/CodeGen/CodeGenModule.h b/lib/CodeGen/CodeGenModule.h index 189fa819..c0843a96 100644 --- a/lib/CodeGen/CodeGenModule.h +++ b/lib/CodeGen/CodeGenModule.h @@ -254,6 +254,7 @@ class CodeGenModule : public CodeGenTypeCache { void emitYulCode(const YulCode *YC, llvm::StringRef Name); void emitYulData(const YulData *YD); + void emitYulDataInMetadata(const YulData *YD); void emitAsmVarDecl(const AsmVarDecl *VD); void emitABILoad(const FunctionDecl *FD, llvm::BasicBlock *Loader, diff --git a/lib/CodeGen/ExprEmitter.cpp b/lib/CodeGen/ExprEmitter.cpp index b4e22c2b..30446c6b 100644 --- a/lib/CodeGen/ExprEmitter.cpp +++ b/lib/CodeGen/ExprEmitter.cpp @@ -66,7 +66,9 @@ ExprValuePtr ExprEmitter::structIndexAccess(const ExprValuePtr StructValue, auto ET = STy->getElementTypes()[ElementIndex]; switch (StructValue->getValueKind()) { case ValueKind::VK_SValue: { - llvm::Value *Base = Builder.CreateLoad(StructValue->getValue()); + llvm::Type *Ty = + StructValue->getValue()->getType()->getPointerElementType(); + llvm::Value *Base = Builder.CreateLoad(Ty, StructValue->getValue()); llvm::Value *Pos = Builder.getIntN(256, STy->getStoragePos(ElementIndex)); llvm::Value *ElemAddress = Builder.CreateAdd(Base, Pos); llvm::Value *Address = Builder.CreateAlloca(CGF.Int256Ty); @@ -80,7 +82,8 @@ ExprValuePtr ExprEmitter::structIndexAccess(const ExprValuePtr StructValue, Indices[0] = llvm::ConstantInt::get(VMContext, llvm::APInt(32, 0, true)); Indices[1] = llvm::ConstantInt::get(VMContext, llvm::APInt(32, ElementIndex, true)); - llvm::Value *ElementPtr = Builder.CreateGEP(Base, Indices); + llvm::Value *ElementPtr = Builder.CreateGEP( + Base->getType()->getPointerElementType(), Base, Indices); return std::make_shared(ET.get(), ValueKind::VK_LValue, ElementPtr); } @@ -114,7 +117,7 @@ ExprValuePtr ExprEmitter::arrayIndexAccess(const ExprValuePtr &Base, return std::make_shared(Ty, ValueKind::VK_LValue, Address); } - llvm::Value *Pos = Builder.CreateLoad(Value); + llvm::Value *Pos = Builder.CreateLoad(CGM.Int256Ty, Value); if (ArrTy->isDynamicSized()) { // load array size and check auto LengthTy = IntegerType::getIntN(256); @@ -596,7 +599,7 @@ ExprValuePtr ExprEmitter::visit(const CastExpr *CE) { llvm::Value *Src = Builder.CreateExtractValue(In, {1}); CGM.emitMemcpy(Dst, Src, Builder.CreateZExtOrTrunc(Length, CGM.Int32Ty)); return ExprValue::getRValue( - CE, CGM.getEndianlessValue(Builder.CreateLoad(Ptr))); + CE, CGM.getEndianlessValue(Builder.CreateLoad(CGM.Int256Ty, Ptr))); } assert(false); break; @@ -710,8 +713,9 @@ ExprValuePtr ExprEmitter::visit(const IndexAccess *IA) { const Type *Ty = IA->getType().get(); if (const auto *MType = dynamic_cast(Base->getType())) { + llvm::Type *LLTy = Base->getValue()->getType()->getPointerElementType(); llvm::Value *Pos = - CGM.getEndianlessValue(Builder.CreateLoad(Base->getValue())); + CGM.getEndianlessValue(Builder.CreateLoad(LLTy, Base->getValue())); llvm::Value *Key; if (MType->getKeyType()->isDynamic()) { Key = Index->load(Builder, CGM); @@ -760,7 +764,7 @@ ExprValuePtr ExprEmitter::visit(const MemberExpr *ME) { llvm::Value *ValPtr = Builder.CreateAlloca(Builder.getInt32Ty()); CGM.emitCallDataCopy(Builder.CreateBitCast(ValPtr, CGF.Int8PtrTy), Builder.getInt32(0), Builder.getInt32(4)); - llvm::Value *Val = Builder.CreateLoad(ValPtr); + llvm::Value *Val = Builder.CreateLoad(Builder.getInt32Ty(), ValPtr); return ExprValue::getRValue(ME, Val); } case Identifier::SpecialIdentifier::tx_gasprice: { diff --git a/lib/Frontend/ASTConsumers/ASTPrinter.cpp b/lib/Frontend/ASTConsumers/ASTPrinter.cpp index eda09b83..a48dba92 100644 --- a/lib/Frontend/ASTConsumers/ASTPrinter.cpp +++ b/lib/Frontend/ASTConsumers/ASTPrinter.cpp @@ -2,9 +2,11 @@ #include "soll/AST/AST.h" #include "soll/AST/ASTConsumer.h" #include "soll/Frontend/ASTConsumers.h" +#if LLVM_VERSION_MAJOR >= 13 +#include +#endif #include #include - namespace { std::string ToString(soll::UnaryOperatorKind op) { @@ -136,7 +138,11 @@ std::string ToString(soll::TypePtr type) { return llvm::Twine(ToString(at->getElementType()) + "[" + (at->isDynamicSized() ? "" +#if LLVM_VERSION_MAJOR >= 13 + : toString(at->getLength(), 10, false)) + +#else : at->getLength().toString(10, false)) + +#endif "]") .str(); } @@ -495,7 +501,11 @@ void ASTPrinter::visit(NumberLiteralType &literal) { const bool Signed = dynamic_cast(literal.getType().get())->isSigned(); os() << indent() << "NumberLiteral " +#if LLVM_VERSION_MAJOR >= 13 + << toString(literal.getValue(), 10, Signed) << "\n"; +#else << literal.getValue().toString(10, Signed) << "\n"; +#endif ConstStmtVisitor::visit(literal); unindent(); } diff --git a/lib/Frontend/CompilerInstance.cpp b/lib/Frontend/CompilerInstance.cpp index 75aaec7d..68d1d36e 100644 --- a/lib/Frontend/CompilerInstance.cpp +++ b/lib/Frontend/CompilerInstance.cpp @@ -278,7 +278,12 @@ std::unique_ptr CompilerInstance::createOutputFile( OSFile = OutFile; OS.reset(new llvm::raw_fd_ostream( OSFile, Error, - (Binary ? llvm::sys::fs::F_None : llvm::sys::fs::F_Text))); +#if LLVM_VERSION_MAJOR >= 13 + (Binary ? llvm::sys::fs::OF_None : llvm::sys::fs::OF_Text) +#else + (Binary ? llvm::sys::fs::F_None : llvm::sys::fs::F_Text) +#endif + )); if (Error) return nullptr; } diff --git a/lib/Frontend/FrontendAction.cpp b/lib/Frontend/FrontendAction.cpp index 9a948ec0..002ca126 100644 --- a/lib/Frontend/FrontendAction.cpp +++ b/lib/Frontend/FrontendAction.cpp @@ -53,7 +53,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI, CI.createASTContext(); - std::string PresumedInputFile = getCurrentFileOrBufferName(); + std::string PresumedInputFile = getCurrentFileOrBufferName().str(); std::unique_ptr Consumer = CreateASTConsumer(CI, PresumedInputFile); diff --git a/lib/Parse/ParseYul.cpp b/lib/Parse/ParseYul.cpp index 2846a5f9..a1019ad4 100644 --- a/lib/Parse/ParseYul.cpp +++ b/lib/Parse/ParseYul.cpp @@ -1,4 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +#include "soll/Basic/DiagnosticIDs.h" +#include "soll/Basic/DiagnosticParse.h" #include "soll/Parse/Parser.h" using namespace std; diff --git a/lib/Parse/Parser.cpp b/lib/Parse/Parser.cpp index ab9db23f..22c6a9ca 100644 --- a/lib/Parse/Parser.cpp +++ b/lib/Parse/Parser.cpp @@ -717,7 +717,7 @@ Parser::parseFunctionHeader(bool ForceEmptyName, bool AllowModifiers) { if (AllowModifiers && Tok.is(tok::identifier)) { Diag(diag::err_unimplemented_token) << tok::identifier; - std::string ModifierName = Tok.getIdentifierInfo()->getName(); + std::string ModifierName = Tok.getIdentifierInfo()->getName().str(); ConsumeToken(); // identifier std::vector Arguments; @@ -963,7 +963,7 @@ Parser::parseVariableDeclaration(VarDeclParserOptions const &Options, std::unique_ptr Parser::parseEventDefinition() { const SourceLocation Begin = Tok.getLocation(); - const std::string Name = Tok.getIdentifierInfo()->getName(); + const std::string Name = Tok.getIdentifierInfo()->getName().str(); ConsumeToken(); // identifier VarDeclParserOptions Options; Options.AllowIndexed = true; @@ -989,7 +989,7 @@ void Parser::parseUserDefinedTypeName() { std::unique_ptr Parser::parseIdentifierPath(tok::TokenKind SplitTok) { assert(Tok.isAnyIdentifier()); - std::vector path{Tok.getIdentifierInfo()->getName()}; + std::vector path{Tok.getIdentifierInfo()->getName().str()}; ConsumeToken(); // identifier while (Tok.is(SplitTok)) { diff --git a/lib/Sema/SemaExprAsm.cpp b/lib/Sema/SemaExprAsm.cpp index 312aecc9..ce572e03 100644 --- a/lib/Sema/SemaExprAsm.cpp +++ b/lib/Sema/SemaExprAsm.cpp @@ -1,5 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception #include "soll/AST/AST.h" +#include "soll/Basic/DiagnosticSema.h" #include "soll/Sema/Sema.h" namespace soll { @@ -674,6 +675,19 @@ Sema::CreateAsmBuiltinCallExpr(SourceRange L, const AsmIdentifier &Callee, return std::make_unique(L, std::move(Args[1]), std::move(Args[0]), std::move(ReturnTy), BO_AShr); + case AsmIdentifier::SpecialIdentifier::dataoffset: + case AsmIdentifier::SpecialIdentifier::datasize: { + if (auto *ICE = dynamic_cast(Args[0].get())) { + if (auto *SL = dynamic_cast(ICE->getSubExpr())) { + std::string Name = SL->getValue(); + if (Name.size() && Name[0] == '.') { + Diag(L.getBegin(), diag::err_yul_access_special_object) << Name; + assert(false); + } + } + } + [[fallthrough]]; + } default: ///< treated as normal CallExpr break; } diff --git a/test/yul/metadata.yul b/test/yul/metadata.yul new file mode 100644 index 00000000..f72f96db --- /dev/null +++ b/test/yul/metadata.yul @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// RUN: %soll --lang=Yul %s +object "metadata_x" { + code { + // sstore(dataoffset(".metadata.x"), 0) + let size := datasize("x") + } + + object ".metadata" { + code {} + data "x" "ABC" + } + + data "x" "CDE" +} diff --git a/tools/soll/CMakeLists.txt b/tools/soll/CMakeLists.txt index 4b14fead..06a254cf 100644 --- a/tools/soll/CMakeLists.txt +++ b/tools/soll/CMakeLists.txt @@ -18,4 +18,5 @@ target_link_libraries(soll sollBasic sollFrontend sollFrontendTool + Threads::Threads )