Index: clang/lib/AST/Interp/ByteCodeEmitter.cpp =================================================================== --- clang/lib/AST/Interp/ByteCodeEmitter.cpp +++ clang/lib/AST/Interp/ByteCodeEmitter.cpp @@ -26,6 +26,7 @@ // Set up argument indices. unsigned ParamOffset = 0; SmallVector ParamTypes; + SmallVector ParamOffsets; llvm::DenseMap ParamDescriptors; // If the return is not a primitive, a pointer to the storage where the @@ -36,6 +37,7 @@ if (!Ty->isVoidType() && !Ctx.classify(Ty)) { HasRVO = true; ParamTypes.push_back(PT_Ptr); + ParamOffsets.push_back(ParamOffset); ParamOffset += align(primSize(PT_Ptr)); } @@ -47,6 +49,7 @@ if (MD->isInstance()) { HasThisPointer = true; ParamTypes.push_back(PT_Ptr); + ParamOffsets.push_back(ParamOffset); ParamOffset += align(primSize(PT_Ptr)); } @@ -74,6 +77,7 @@ Descriptor *Desc = P.createDescriptor(PD, Ty); ParamDescriptors.insert({ParamOffset, {Ty, Desc}}); Params.insert({PD, ParamOffset}); + ParamOffsets.push_back(ParamOffset); ParamOffset += align(primSize(Ty)); ParamTypes.push_back(Ty); } @@ -81,9 +85,9 @@ // Create a handle over the emitted code. Function *Func = P.getFunction(FuncDecl); if (!Func) - Func = - P.createFunction(FuncDecl, ParamOffset, std::move(ParamTypes), - std::move(ParamDescriptors), HasThisPointer, HasRVO); + Func = P.createFunction(FuncDecl, ParamOffset, std::move(ParamTypes), + std::move(ParamDescriptors), + std::move(ParamOffsets), HasThisPointer, HasRVO); assert(Func); // For not-yet-defined functions, we only create a Function instance and Index: clang/lib/AST/Interp/Function.h =================================================================== --- clang/lib/AST/Interp/Function.h +++ clang/lib/AST/Interp/Function.h @@ -156,12 +156,17 @@ unsigned getNumParams() const { return ParamTypes.size(); } + unsigned getParamOffset(unsigned ParamIndex) const { + return ParamOffsets[ParamIndex]; + } + private: /// Construct a function representing an actual function. Function(Program &P, const FunctionDecl *F, unsigned ArgSize, llvm::SmallVector &&ParamTypes, llvm::DenseMap &&Params, - bool HasThisPointer, bool HasRVO); + llvm::SmallVector &&ParamOffsets, bool HasThisPointer, + bool HasRVO); /// Sets the code of a function. void setCode(unsigned NewFrameSize, std::vector &&NewCode, @@ -201,6 +206,8 @@ llvm::SmallVector ParamTypes; /// Map from byte offset to parameter descriptor. llvm::DenseMap Params; + /// List of parameter offsets. + llvm::SmallVector ParamOffsets; /// Flag to indicate if the function is valid. bool IsValid = false; /// Flag to indicate if the function is done being Index: clang/lib/AST/Interp/Function.cpp =================================================================== --- clang/lib/AST/Interp/Function.cpp +++ clang/lib/AST/Interp/Function.cpp @@ -18,10 +18,12 @@ Function::Function(Program &P, const FunctionDecl *F, unsigned ArgSize, llvm::SmallVector &&ParamTypes, llvm::DenseMap &&Params, + llvm::SmallVector &&ParamOffsets, bool HasThisPointer, bool HasRVO) : P(P), Loc(F->getBeginLoc()), F(F), ArgSize(ArgSize), ParamTypes(std::move(ParamTypes)), Params(std::move(Params)), - HasThisPointer(HasThisPointer), HasRVO(HasRVO) {} + ParamOffsets(std::move(ParamOffsets)), HasThisPointer(HasThisPointer), + HasRVO(HasRVO) {} Function::ParamDescriptor Function::getParamDescriptor(unsigned Offset) const { auto It = Params.find(Offset); Index: clang/lib/AST/Interp/Interp.h =================================================================== --- clang/lib/AST/Interp/Interp.h +++ clang/lib/AST/Interp/Interp.h @@ -155,7 +155,7 @@ bool Interpret(InterpState &S, APValue &Result); /// Interpret a builtin function. -bool InterpretBuiltin(InterpState &S, CodePtr &PC, unsigned BuiltinID); +bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F); /// Perform a bitcast of all fields of P into Buff. This performs the /// actions of a __builtin_bit_cast expression when the target type @@ -1748,7 +1748,7 @@ InterpFrame *FrameBefore = S.Current; S.Current = NewFrame.get(); - if (InterpretBuiltin(S, PC, Func->getBuiltinID())) { + if (InterpretBuiltin(S, PC, Func)) { NewFrame.release(); return true; } Index: clang/lib/AST/Interp/InterpBuiltin.cpp =================================================================== --- clang/lib/AST/Interp/InterpBuiltin.cpp +++ clang/lib/AST/Interp/InterpBuiltin.cpp @@ -14,15 +14,64 @@ namespace clang { namespace interp { -bool InterpretBuiltin(InterpState &S, CodePtr &PC, unsigned BuiltinID) { +template T getParam(InterpFrame *Frame, unsigned Index) { + unsigned Offset = Frame->getFunction()->getParamOffset(Index); + return Frame->getParam(Offset); +} + +static bool interp__builtin_strcmp(InterpState &S, CodePtr OpPC, + InterpFrame *Frame) { + const Pointer &A = getParam(Frame, 0); + const Pointer &B = getParam(Frame, 1); + + if (!CheckLive(S, OpPC, A, AK_Read) || !CheckLive(S, OpPC, B, AK_Read)) + return false; + + assert(A.getFieldDesc()->isPrimitiveArray()); + assert(B.getFieldDesc()->isPrimitiveArray()); + + unsigned IndexA = A.getIndex(); + unsigned IndexB = B.getIndex(); + int32_t Result = 0; + for (;; ++IndexA, ++IndexB) { + const Pointer &PA = A.atIndex(IndexA); + const Pointer &PB = B.atIndex(IndexB); + if (!CheckRange(S, OpPC, PA, AK_Read) || + !CheckRange(S, OpPC, PB, AK_Read)) { + return false; + } + uint8_t CA = PA.deref(); + uint8_t CB = PB.deref(); + + if (CA > CB) { + Result = 1; + break; + } else if (CA < CB) { + Result = -1; + break; + } + if (CA == 0 || CB == 0) + break; + } + + S.Stk.push>(Integral<32, true>::from(Result)); + return true; +} + +bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F) { + InterpFrame *Frame = S.Current; APValue Dummy; - switch (BuiltinID) { + switch (F->getBuiltinID()) { case Builtin::BI__builtin_is_constant_evaluated: S.Stk.push(Boolean::from(S.inConstantContext())); - return Ret(S, PC, Dummy); + return Ret(S, OpPC, Dummy); case Builtin::BI__builtin_assume: - return RetVoid(S, PC, Dummy); + return RetVoid(S, OpPC, Dummy); + case Builtin::BI__builtin_strcmp: + if (interp__builtin_strcmp(S, OpPC, Frame)) + return Ret(S, OpPC, Dummy); + return false; default: return false; } Index: clang/lib/AST/Interp/Pointer.h =================================================================== --- clang/lib/AST/Interp/Pointer.h +++ clang/lib/AST/Interp/Pointer.h @@ -341,7 +341,8 @@ /// Dereferences a primitive element. template T &elem(unsigned I) const { - return reinterpret_cast(Pointee->rawData())[I]; + assert(I < getNumElems()); + return reinterpret_cast(Pointee->data() + sizeof(InitMap *))[I]; } /// Initializes a field. Index: clang/test/AST/Interp/builtin-functions.cpp =================================================================== --- /dev/null +++ clang/test/AST/Interp/builtin-functions.cpp @@ -0,0 +1,35 @@ +// RUN: %clang_cc1 -fexperimental-new-constant-interpreter %s -verify +// RUN: %clang_cc1 -verify=ref %s -Wno-constant-evaluated + +namespace strcmp { + constexpr char kFoobar[6] = {'f','o','o','b','a','r'}; + constexpr char kFoobazfoobar[12] = {'f','o','o','b','a','z','f','o','o','b','a','r'}; + + static_assert(__builtin_strcmp("abab", "abab") == 0); + static_assert(__builtin_strcmp("abab", "abba") == -1); + static_assert(__builtin_strcmp("abab", "abaa") == 1); + static_assert(__builtin_strcmp("ababa", "abab") == 1); + static_assert(__builtin_strcmp("abab", "ababa") == -1); + static_assert(__builtin_strcmp("a\203", "a") == 1); + static_assert(__builtin_strcmp("a\203", "a\003") == 1); + static_assert(__builtin_strcmp("abab\0banana", "abab") == 0); + static_assert(__builtin_strcmp("abab", "abab\0banana") == 0); + static_assert(__builtin_strcmp("abab\0banana", "abab\0canada") == 0); + static_assert(__builtin_strcmp(0, "abab") == 0); // expected-error {{not an integral constant}} \ + // expected-note {{dereferenced null}} \ + // expected-note {{in call to}} \ + // ref-error {{not an integral constant}} \ + // ref-note {{dereferenced null}} + static_assert(__builtin_strcmp("abab", 0) == 0); // expected-error {{not an integral constant}} \ + // expected-note {{dereferenced null}} \ + // expected-note {{in call to}} \ + // ref-error {{not an integral constant}} \ + // ref-note {{dereferenced null}} + + static_assert(__builtin_strcmp(kFoobar, kFoobazfoobar) == -1); + static_assert(__builtin_strcmp(kFoobar, kFoobazfoobar + 6) == 0); // expected-error {{not an integral constant}} \ + // expected-note {{dereferenced one-past-the-end}} \ + // expected-note {{in call to}} \ + // ref-error {{not an integral constant}} \ + // ref-note {{dereferenced one-past-the-end}} +}