Commit Graph

171 Commits

Author SHA1 Message Date
Timm Bäder 89b009559c Revert "[clang][Interp] Use placement new to construct opcode args into vector"
This reverts commit aaf73ae266.

This breaks sanitized builds because the constructor is called with an
unaligned address.
2022-11-30 16:07:57 +01:00
Timm Bäder ec23e5584d [clang][Interp][NFC] Avoid unnecessary work in compileFunc()
We don't need to create the paramter descriptors etc. if we've already
done that in the past.
2022-11-30 16:07:29 +01:00
Timm Bäder 7c215a4571 [clang][Interp] Explicitly handle RVO Pointer
The calling convention is:

[RVO pointer]
[instance pointer]
[... args ...]

We handle the instance pointer ourselves, BUT for the RVO pointer, we
just assumed in visitReturnStmt() that it is on top of the stack. Which
isn't true if there are other args present (and a this pointer, maybe).

Fix this by recording the RVO pointer explicitly when creating an
InterpFrame, just like we do with the instance/This pointer.

There is already a "RVOAndParams()" test in test/AST/Inter/records.cpp,
that was supposed to test this, however, it didn't trigger any
problematic behavior because the parameter and the return value have the
same type.

Differential Revision: https://reviews.llvm.org/D137392
2022-11-30 10:37:57 +01:00
Timm Bäder aaf73ae266 [clang][Interp] Use placement new to construct opcode args into vector
This way we're invoking the copy constructor, which might be necessary
if the argument is not trivially constructible.

Differential Revision: https://reviews.llvm.org/D138554
2022-11-30 10:09:52 +01:00
Timm Bäder c3380c32f8 [clang][Interp] Handle undefined functions better
Differential Revision: https://reviews.llvm.org/D136936
2022-11-30 10:09:52 +01:00
Timm Bäder 8095b090db [clang][Interp] Array initialization via CXXConstructExpr
Differential Revision: https://reviews.llvm.org/D136920
2022-11-30 10:09:52 +01:00
Timm Bäder afa60d08a2 [clang][Interp] Fix discarding non-primitive function call return values
Differential Revision: https://reviews.llvm.org/D136457
2022-11-30 09:18:28 +01:00
Kazu Hirata 30f9eb1eb8 [clang] Remove unused forward declarations (NFC) 2022-11-20 14:32:17 -08:00
Timm Bäder d128a03ff3 [clang][Interp][NFC] Use constexpr if in OffsetHelper
Add here is a template parameter, so we can do this.
2022-11-16 09:56:29 +01:00
Timm Bäder 99d3ead44c [clang][Interp] Protect Record creation against infinite recursion
This happens only in error cases, but we need to handle it anyway.

Differential Revision: https://reviews.llvm.org/D136831
2022-11-11 08:38:06 +01:00
Timm Bäder 0dcfd0ce02 [clang][Interp] Support alignof()
Support alignof() and __alignof() expressions.

Fixes #58816

Differential Revision: https://reviews.llvm.org/D137240
2022-11-11 08:38:06 +01:00
Timm Bäder 7863646fd2 [clang][Interp] DerivedToBase casts
Differential Revision: https://reviews.llvm.org/D137545
2022-11-11 08:38:06 +01:00
Nathan James 108e41d962
[clang][NFC] Use c++17 style variable type traits
This was done as a test for D137302 and it makes sense to push these changes

Reviewed By: shafik

Differential Revision: https://reviews.llvm.org/D137491
2022-11-07 18:25:48 +00:00
Timm Bäder 5dfacb1245 [clang][Interp][NFC] Replace dyn_cast_or_null with _if_present
... in Descriptor.h
2022-11-07 09:42:41 +01:00
Timm Bäder 5bd6bd1227 [clang][Interp][NFC] Simplify visitReturnStmt() 2022-11-07 09:42:41 +01:00
Timm Bäder 6b3e5c595b [clang][Interp][NFC] Remove unused function 2022-11-07 09:42:41 +01:00
Timm Bäder c8341a6615 [clang][Interp][NFC] Avoid a getSource() call in the common case
In the common (successful) case, we don't need the getSource() call, so
move it to the two if statement bodies instead.
2022-11-07 09:42:41 +01:00
Timm Bäder f4707af294 [clang][Interp][NFCI] Cleanup emitConst()
Before, when emitting a regular integer constant, we went:

Int -> APInt -> int -> emit

Fix this by using regular integer constants in emitConst() and instead
converting APInt to those once.
2022-11-07 09:05:29 +01:00
Timm Bäder 05a113e188 [clang][Interp][NFC] Handle discarded ArraySubscriptExprs
This is not exactly a common case, so just pop the pointer at the end if
necessary.
2022-11-07 08:37:43 +01:00
Timm Bäder 9a3b969d1f [clang][Interp][NFC] Make InitField() not pop the pointer
This was confusing. InitElem peeks a pointer, while InitElemPop will
pop the pointer. However, for fields, InitField would pop the pointer
and no InitFieldPop exists. At least make InitField and InitElem behave
the same.
2022-11-07 08:30:43 +01:00
Timm Bäder 7c0a2d9cda [clang][Interp][NFC] Use StorePop for assignments with DiscardResult
If we don't need the result anyway, use StorePop, instead of a Store+Pop
combination. That way we save one instruction and not using the result
is the common case anyway.
2022-11-07 07:56:25 +01:00
Timm Bäder 10483ac743 [clang][Interp] Support pointer arithmethic in binary operators
Differential Revision: https://reviews.llvm.org/D135858
2022-11-07 07:47:19 +01:00
Timm Bäder 27f5f33c81 [clang][Interp][NFC] Remove an unused include
And an unnecessary private marker.
2022-11-07 06:34:22 +01:00
Aaron Ballman 37f80101a9 Silence a "not all control paths return" MSVC warning; NFC 2022-11-03 10:27:16 -04:00
Benjamin Kramer da4b929da7 [clang][Interp] Reorder field destruction to avoid use after dtor
Found by msan -fsanitize-memory-use-after-dtor.

==8259==WARNING: MemorySanitizer: use-of-uninitialized-value
    #0 0x55dbec54d2b8 in dtorRecord(clang::interp::Block*, char*, clang::interp::Descriptor*) clang/lib/AST/Interp/Descriptor.cpp:150:22
    #1 0x55dbec54bfcf in dtorArrayDesc(clang::interp::Block*, char*, clang::interp::Descriptor*) clang/lib/AST/Interp/Descriptor.cpp:97:7
    #2 0x55dbec508578 in invokeDtor clang/lib/AST/Interp/InterpBlock.h:79:7
    #3 0x55dbec508578 in clang::interp::Program::~Program() clang/lib/AST/Interp/Program.h:55:19
    #4 0x55dbec50657a in operator() third_party/crosstool/v18/stable/toolchain/bin/../include/c++/v1/__memory/unique_ptr.h:55:5
    #5 0x55dbec50657a in std::__msan::unique_ptr<clang::interp::Program, std::__msan::default_delete<clang::interp::Program>>::~unique_ptr() third_party/crosstool/v18/stable/toolchain/bin/../include/c++/v1/__memory/unique_ptr.h:261:7
    #6 0x55dbec5035a1 in clang::interp::Context::~Context() clang/lib/AST/Interp/Context.cpp:27:22
    #7 0x55dbebec1daa in operator() third_party/crosstool/v18/stable/toolchain/bin/../include/c++/v1/__memory/unique_ptr.h:55:5
    #8 0x55dbebec1daa in std::__msan::unique_ptr<clang::interp::Context, std::__msan::default_delete<clang::interp::Context>>::~unique_ptr() third_party/crosstool/v18/stable/toolchain/bin/../include/c++/v1/__memory/unique_ptr.h:261:7
    #9 0x55dbebe285f9 in clang::ASTContext::~ASTContext() clang/lib/AST/ASTContext.cpp:1038:40
    #10 0x55dbe941ff13 in llvm::RefCountedBase<clang::ASTContext>::Release() const llvm/include/llvm/ADT/IntrusiveRefCntPtr.h:101:7
    #11 0x55dbe94353ef in release llvm/include/llvm/ADT/IntrusiveRefCntPtr.h:159:38
    #12 0x55dbe94353ef in release llvm/include/llvm/ADT/IntrusiveRefCntPtr.h:224:7
    #13 0x55dbe94353ef in ~IntrusiveRefCntPtr llvm/include/llvm/ADT/IntrusiveRefCntPtr.h:191:27
    #14 0x55dbe94353ef in clang::CompilerInstance::setASTContext(clang::ASTContext*) clang/lib/Frontend/CompilerInstance.cpp:178:3
    #15 0x55dbe95ad0ad in clang::FrontendAction::EndSourceFile() clang/lib/Frontend/FrontendAction.cpp:1100:8
    #16 0x55dbe9445fcf in clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) clang/lib/Frontend/CompilerInstance.cpp:1047:11
    #17 0x55dbe6b3afef in clang::ExecuteCompilerInvocation(clang::CompilerInstance*) clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp:266:25
    #18 0x55dbe6b13288 in cc1_main(llvm::ArrayRef<char const*>, char const*, void*) clang/tools/driver/cc1_main.cpp:250:15
    #19 0x55dbe6b0095f in ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&) clang/tools/driver/driver.cpp:319:12
    #20 0x55dbe6aff41c in clang_main(int, char**) clang/tools/driver/driver.cpp:395:12
    #21 0x7f9be07fa632 in __libc_start_main
    #22 0x55dbe6a702e9 in _start

  Member fields were destroyed
    #0 0x55dbe6a7da5d in __sanitizer_dtor_callback_fields compiler-rt/lib/msan/msan_interceptors.cpp:949:5
    #1 0x55dbec5094ac in ~SmallVectorImpl llvm/include/llvm/ADT/SmallVector.h:479:7
    #2 0x55dbec5094ac in ~SmallVectorImpl llvm/include/llvm/ADT/SmallVector.h:612:3
    #3 0x55dbec5094ac in llvm::SmallVector<clang::interp::Record::Base, 8u>::~SmallVector() llvm/include/llvm/ADT/SmallVector.h:1207:3
    #4 0x55dbec508e79 in clang::interp::Record::~Record() clang/lib/AST/Interp/Record.h:24:7
    #5 0x55dbec508612 in clang::interp::Program::~Program() clang/lib/AST/Interp/Program.h:49:26
    #6 0x55dbec50657a in operator() third_party/crosstool/v18/stable/toolchain/bin/../include/c++/v1/__memory/unique_ptr.h:55:5
    #7 0x55dbec50657a in std::__msan::unique_ptr<clang::interp::Program, std::__msan::default_delete<clang::interp::Program>>::~unique_ptr() third_party/crosstool/v18/stable/toolchain/bin/../include/c++/v1/__memory/unique_ptr.h:261:7
    #8 0x55dbec5035a1 in clang::interp::Context::~Context() clang/lib/AST/Interp/Context.cpp:27:22
    #9 0x55dbebec1daa in operator() third_party/crosstool/v18/stable/toolchain/bin/../include/c++/v1/__memory/unique_ptr.h:55:5
    #10 0x55dbebec1daa in std::__msan::unique_ptr<clang::interp::Context, std::__msan::default_delete<clang::interp::Context>>::~unique_ptr() third_party/crosstool/v18/stable/toolchain/bin/../include/c++/v1/__memory/unique_ptr.h:261:7
    #11 0x55dbebe285f9 in clang::ASTContext::~ASTContext() clang/lib/AST/ASTContext.cpp:1038:40
    #12 0x55dbe941ff13 in llvm::RefCountedBase<clang::ASTContext>::Release() const llvm/include/llvm/ADT/IntrusiveRefCntPtr.h:101:7
    #13 0x55dbe94353ef in release llvm/include/llvm/ADT/IntrusiveRefCntPtr.h:159:38
    #14 0x55dbe94353ef in release llvm/include/llvm/ADT/IntrusiveRefCntPtr.h:224:7
    #15 0x55dbe94353ef in ~IntrusiveRefCntPtr llvm/include/llvm/ADT/IntrusiveRefCntPtr.h:191:27
    #16 0x55dbe94353ef in clang::CompilerInstance::setASTContext(clang::ASTContext*) clang/lib/Frontend/CompilerInstance.cpp:178:3
    #17 0x55dbe95ad0ad in clang::FrontendAction::EndSourceFile() clang/lib/Frontend/FrontendAction.cpp:1100:8
    #18 0x55dbe9445fcf in clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) clang/lib/Frontend/CompilerInstance.cpp:1047:11
    #19 0x55dbe6b3afef in clang::ExecuteCompilerInvocation(clang::CompilerInstance*) clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp:266:25
    #20 0x55dbe6b13288 in cc1_main(llvm::ArrayRef<char const*>, char const*, void*) clang/tools/driver/cc1_main.cpp:250:15
    #21 0x55dbe6b0095f in ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&) clang/tools/driver/driver.cpp:319:12
    #22 0x55dbe6aff41c in clang_main(int, char**) clang/tools/driver/driver.cpp:395:12
    #23 0x7f9be07fa632 in __libc_start_main
    #24 0x55dbe6a702e9 in _start
2022-10-31 12:20:10 +01:00
Timm Bäder 74fb770de9 [clang][Interp] Implement bitXor opcode
Differential Revision: https://reviews.llvm.org/D136956
2022-10-30 09:23:33 +01:00
Timm Bäder 6d965c94ba [clang][Interp] Implement left and right shifts
Differential Revision: https://reviews.llvm.org/D136532
2022-10-30 08:59:55 +01:00
Timm Bäder 881547db03 [clang][Interp] Implement add and sub compound assign operators
Differential Revision: https://reviews.llvm.org/D136528
2022-10-30 08:11:04 +01:00
Timm Bäder f79f64b8d0 [clang][Interp] Implement inc and dec operators
Differential Revision: https://reviews.llvm.org/D136423
2022-10-28 17:47:32 +02:00
Timm Bäder ddec89670d [clang][Interp][NFC] Use right visit() function
visit (lowercase V) sets DiscardValue to false and calls Visit
(uppercase V). So we can't just call Visit (uppercase V) ourselves,
since then we aren't handling DiscardValue correctly.
This is currently irrelevant but will make a difference later.

Also, the naming isn't my fault and might change later.
2022-10-28 17:44:53 +02:00
Timm Bäder ee9bbfa5e6 [clang][Interp] Make sure we free() allocated InitMaps
They get allocated when calling initialize() on a primitive array. And
they get free'd when the array is fully initialized. However, when that
never happens, they get leaked. Fix that by calling the destructor of
global variables.

Differential Revision: https://reviews.llvm.org/D136826
2022-10-28 17:00:15 +02:00
Timm Bäder 38ffc89c87 [clang][Interp] Fix ignoring expression return values
Randomly noticed this. We need to honor DiscardResult here.

Differential Revision: https://reviews.llvm.org/D136013
2022-10-28 16:45:38 +02:00
Timm Bäder 0ccff030f3 [clang][Interp] Fix record members of reference type
When assigning to them, we can't classify the expression type, because
that doesn't contain the right information.

And when reading from them, we need to do the extra deref, just like we
do when reading from a DeclRefExpr.

Differential Revision: https://reviews.llvm.org/D136012
2022-10-28 16:34:45 +02:00
Timm Bäder 8b87cb4853 [clang][Interp] Remove unused getGlobalIdx()
Remove the only use with the version we already use in
VisitDeclRefExpr().
2022-10-28 16:34:29 +02:00
Martin Storsjö 83b97b3b27 [clang] Fix a -Wcast-qual GCC warning. NFC.
This fixes the following warning:

../tools/clang/lib/AST/Interp/Disasm.cpp: In member function ‘void clang::interp::Function::dump(llvm::raw_ostream&) const’:
../tools/clang/lib/AST/Interp/Disasm.cpp:43:25: warning: cast from type ‘const clang::interp::Function*’ to type ‘void*’ casts away qualifiers [-Wcast-qual]
   43 |     OS << " " << (void*)this << ":\n";
      |                         ^~~~
2022-10-28 16:11:34 +03:00
Timm Bäder 52ba50c24c [clang][Interp] Implement String- and CharacterLiterals
Differential Revision: https://reviews.llvm.org/D135366
2022-10-28 15:05:21 +02:00
Timm Bäder 63e3fe1088 Revert "[clang][Interp] Implement String- and CharacterLiterals"
This reverts commit 6fa1795d1f.

This breaks a windows builder:

../../clang/lib/AST/Interp/ByteCodeExprGen.h(236,11): error: call to constructor of 'clang::interp::APInt' (aka 'llvm::APInt') is ambiguous
    APInt WrappedValue(getIntWidth(Ty), Value, std::is_signed<T>::value);
          ^            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../clang/lib/AST/Interp/ByteCodeExprGen.cpp(409,16): note: in instantiation of function template specialization 'clang::interp::ByteCodeExprGen<clang::interp::ByteCodeEmitter>::emitConst<unsigned int>' requested here
  return this->emitConst(E, E->getValue());
               ^
../../clang/lib/AST/Interp/ByteCodeExprGen.cpp(1149,16): note: in instantiation of member function 'clang::interp::ByteCodeExprGen<clang::interp::ByteCodeEmitter>::VisitCharacterLiteral' requested here
template class ByteCodeExprGen<ByteCodeEmitter>;
               ^
../../llvm/include\llvm/ADT/APInt.h(108,3): note: candidate constructor
  APInt(unsigned numBits, uint64_t val, bool isSigned = false)
  ^
../../llvm/include\llvm/ADT/APInt.h(134,3): note: candidate constructor
  APInt(unsigned numBits, unsigned numWords, const uint64_t bigVal[]);
  ^
2022-10-28 13:34:37 +02:00
Timm Bäder 7f3013c5f9 [clang][Interp][NFC] Fix instance method check
This was a change suggested by Aaron that I forgot to commit.
2022-10-28 12:49:38 +02:00
Timm Bäder 6fa1795d1f [clang][Interp] Implement String- and CharacterLiterals
Differential Revision: https://reviews.llvm.org/D135366
2022-10-28 12:49:38 +02:00
Timm Bäder c1dd7f9013 [clang][Interp][NFC] Print more info in Program::dump()
Number of functions and global variables is useful to know.
2022-10-28 12:49:38 +02:00
Timm Bäder 0ad3ad6bc7 [clang][Interp][NFC] Remove leftover comment fragment 2022-10-28 12:49:38 +02:00
Timm Bäder 6e7f421e55 [clang][Interp][NFC] Fix a file header comment 2022-10-28 12:49:38 +02:00
Timm Bäder b61e8c517e [clang][Interp][NFC] Add Record::getName()
Useful for debugging.
2022-10-28 12:49:34 +02:00
Timm Bäder caa83d2329 [clang][Interp][NFC] Simplify InterpFrame::describe 2022-10-26 08:49:44 +02:00
Timm Bäder 751f1bfb5f [clang][Interp][NFC] Assert we have a valid Record instance 2022-10-26 08:49:31 +02:00
Timm Bäder 4a9b727326 [clang][Interp][NFC] Make InitMap final 2022-10-26 08:49:20 +02:00
Timm Bäder ea2596692f [clang][Interp][NFC] Simplify generated code for references
Instead of getting a pointer to a pointer to a value, followed by
dereferencing once, leaving us with a pointer to a value, we can instead
just get the pointer to the value (the reference in question) directly.
This simplifies (and shrinks) the generated bytecode somewhat.
2022-10-26 08:49:11 +02:00
Timm Bäder f007c99ed1 [clang][Interp] Fix InterpFrame::describe() for This pointers
Since we pass this via the stack, we need to account for it when
desribing the stack frame (parameters). This fixes a previously
commented-out test case.

Differential Revision: https://reviews.llvm.org/D136670
2022-10-26 08:48:55 +02:00
Timm Bäder f6f02e0900 [clang][Interp] Implement for loops
Differential Revision: https://reviews.llvm.org/D135764
2022-10-26 08:48:36 +02:00
Kazu Hirata f92980c726 [clang] Fix a warning
This patch fixes:

  clang/lib/AST/Interp/ByteCodeExprGen.cpp:978:24: warning: variable
  ‘T’ set but not used [-Wunused-but-set-variable]

T and ReturnType were introduced on August 19, 2022 in commit
8e41e6a4ea.

Their last uses were removed on October 13, 2022 in commit
0e754cfadc.
2022-10-23 09:34:49 -07:00