130 Commits

Author SHA1 Message Date
Timm Bäder
d6b0af0574 [clang][Interp] Add more shift error checking
Differential Revision: https://reviews.llvm.org/D150209
2023-07-20 15:35:47 +02:00
Timm Bäder
0d40973644 [clang][Interp][NFC] Move CastFP to Interp.h
It's not a Check* function, so try to stay consistent and move this to
the header.
2023-07-04 17:35:28 +02:00
Timm Bäder
74514e8713 [clang][Interp][NFC] Fix GetFnPtr signature 2023-07-04 17:35:28 +02:00
Timm Bäder
459f495f49 [clang][Interp] Check inc/dec family of ops for initialization
Differential Revision: https://reviews.llvm.org/D149846
2023-06-16 15:19:23 +02:00
Timm Bäder
461f91b1e4 [clang][Interp] Handle LambdaExprs
Differential Revision: https://reviews.llvm.org/D146030
2023-06-16 10:05:33 +02:00
Timm Bäder
976d8b40cc [clang][Interp] Virtual function calls
Add a CallVirt opcode and implement virtual function calls this way.

Differential Revision: https://reviews.llvm.org/D142630
2023-06-15 13:33:43 +02:00
Timm Bäder
da836b36bc [clang][Interp] Track frame depth
Save the depth of each InterpFrame and bail out if we're too deep.

Differential Revision: https://reviews.llvm.org/D148614
2023-05-31 12:08:03 +02:00
Timm Bäder
338c2489f6 [clang][Interp] Implement inc/dec operators for floats
Differential Revision: https://reviews.llvm.org/D149634
2023-05-04 08:52:53 +02:00
Timm Bäder
9ec5423b90 [clang][Interp][NFC] Add documentation for SetLocal 2023-05-02 06:16:49 +02:00
Timm Bäder
f8a9c55bef [clang][Interp] Emit diagnostic when comparing function pointers
Function pointers can be compared for (in)equality but, but LE, GE, LT,
and GT opcodes should emit an error and abort.

Differential Revision: https://reviews.llvm.org/D149154
2023-04-27 12:33:28 +02:00
Timm Bäder
6cf14a7239 [clang][Interp] Check Neg ops for errors
This should fail when negating __INT_MIN__.

Differential Revision: https://reviews.llvm.org/D148987
2023-04-27 12:05:23 +02:00
Timm Bäder
3185e47b5a [clang][Interp] Fix post-inc/dec operator result
We pushed the wrong value on the stack, always leaving a 0 behind.

Differential Revision: https://reviews.llvm.org/D148901
2023-04-26 09:36:08 +02:00
Timm Bäder
e7f55bbdb8 [clang][Interp][NFCI] Call* ops don't modify the PC
This caused the reported errors from the Call*() handlers to report the
wrong source location.

Fixes: https://github.com/llvm/llvm-project/issues/62002
2023-04-08 08:49:22 +02:00
Timm Bäder
943ef06420 [clang][Interp] Check This pointer without creating InterpFrame
The InterpFrame was only created so early so we could use getThis().
However, we need to know the Function when creating the InterpFrame and
in the case of virtual functions, we need to determine what function to
call at interpretation time.

Get the This pointer ourselves and just create the InterpFrame later.

Differential Revision: https://reviews.llvm.org/D142617
2023-03-31 16:18:15 +02:00
Timm Bäder
3ad167329a [clang][Interp] Implement function pointers
Differential Revision: https://reviews.llvm.org/D141472
2023-03-30 15:37:49 +02:00
Timm Bäder
6b5afda61c [clang][Interp] Add ArrayElemPtr{,Pop} opcode
We usually access array elements in the same pattern, which uses
narrow(). Add an extra opcode for this. This saves us quite some
instructions and makes the bytecode easier to read.

Differential Revision: https://reviews.llvm.org/D140805
2023-03-02 09:45:18 +01:00
Timm Bäder
569222e172 [clang][Interp] Only check constructors for global variables
Local variables may be partially initialized.

Differential Revision: https://reviews.llvm.org/D140723
2023-03-02 09:13:47 +01:00
Timm Bäder
7bb615ea0e [clang][Interp] Materializing primitive temporaries
Implement MaterializeTemporaryExpr for primitive types.

Differential Revision: https://reviews.llvm.org/D136017
2023-02-03 15:11:15 +01:00
Timm Bäder
7b966e2156 [clang][Interp][NFC] Add GetPtrBasePop opcode
Change GetPtrBase to *not* pop the base pointer and add a *Pop variant.
This will be used in later patches.
2023-01-27 08:05:53 +01:00
Timm Bäder
760136ff13 [clang][Interp] Implement __builtin_assume
Just ignore it.

As part of this, move the Ret and RetVoid implementation to Interp.h, so
they can be shared with InterpBuiltin.cpp.

Differential Revision: https://reviews.llvm.org/D141193
2023-01-27 07:54:37 +01:00
Timm Bäder
7f48154ca1 Re-apply "[clang][Interp] Fix left-/right-shifting more than sizeof(unsigned)" 2023-01-26 08:02:27 +01:00
Timm Bäder
136c7ef5d1 Revert "[clang][Interp] Fix left-/right-shifting more than sizeof(unsigned)"
This reverts commit 00e967f6c2d626d1913f5af5763beab7946978ce.

This breaks builders where long is only 32 bits, e.g.
https://lab.llvm.org/buildbot/#/builders/65/builds/7721
https://lab.llvm.org/buildbot/#/builders/245/builds/3899
2023-01-26 07:49:10 +01:00
Timm Bäder
00e967f6c2 [clang][Interp] Fix left-/right-shifting more than sizeof(unsigned)
We were just casting to `unsigned` before, so that caused problems when
shifting more bits than `unsigned` has.

Differential Revision: https://reviews.llvm.org/D140845
2023-01-26 07:07:17 +01:00
Timm Bäder
93aa412306 [clang][Interp][NFC] Refector OffsetHelper
There was a FIXME comment for this. Stop getting the values in
OffsetHelper and let the caller do that instead, so we can control
whether the value(s) are removed from the stack at all.

Also use ArithOp instead of the unclear boolean for Add.
2023-01-25 17:22:59 +01:00
Timm Bäder
cb703434cb [clang][Interp] Add back Run() call
We need to run the functions we compiled immediately after to check if
they can ever be a constant expression.

Differential Revision: https://reviews.llvm.org/D140724
2023-01-25 17:01:55 +01:00
Timm Bäder
cb7f582211 Re-apply "[clang][Interp] Support floating-point values"
Don't move the Repr struct into Integral this time.

Differential Revision: https://reviews.llvm.org/D134859
2023-01-25 15:13:09 +01:00
Timm Bäder
b3b1d86137 Revert "[clang][Interp] Support floating-point values"
This reverts commit 62f43c3eae2460d4ca3da7897fd2d7c56920638c.

This breaks a couple of builders, e.g.

https://lab.llvm.org/buildbot/#/builders/139/builds/34925
2023-01-25 14:48:39 +01:00
Timm Bäder
62f43c3eae [clang][Interp] Support floating-point values
Add a new Floating type and a few new opcodes to support floating point
values.

Differential Revision: https://reviews.llvm.org/D134859
2023-01-25 14:41:26 +01:00
Timm Bäder
a7a4463acb [clang][Interp] Start implementing builtin functions
Differential Revision: https://reviews.llvm.org/D137487
2023-01-25 14:08:03 +01:00
Timm Bäder
7348bb36c5 [clang][Interp] Support inc/dec operators for pointers
Differential Revision: https://reviews.llvm.org/D137232
2023-01-25 11:57:05 +01:00
Timm Bäder
b07399f0fa [clang][Interp][NFC] Forward-declare Boolean in PrimTypes.h
We don't need the full header file here.
2023-01-22 08:33:39 +01:00
Timm Bäder
0f5a81cb44 [clang][Interp][NFC] Remove shift error checking code duplication 2023-01-19 13:52:47 +01:00
Timm Bäder
85acfc6425 Revert "[clang][Interp][NFC] Remove shift error checking code duplication"
This reverts commit fddf6418e8492a544c9bfdb42a4dbc949d9dc2ee.

Apparently this also breaks some builders:

/usr/bin/ld: EvalEmitter.cpp:(.text._ZN5clang6interp11EvalEmitter7emitShlENS0_8PrimTypeES2_RKNS0_10SourceInfoE+0x1f54): undefined reference to `bool clang::interp::CheckShift<clang::interp::Integral<16u, true> >(clang::interp::InterpState&, clang::interp::CodePtr, clang::interp::Integral<16u, true> const&, unsigned int)'
/usr/bin/ld: EvalEmitter.cpp:(.text._ZN5clang6interp11EvalEmitter7emitShlENS0_8PrimTypeES2_RKNS0_10SourceInfoE+0x1fd4): undefined reference to `bool clang::interp::CheckShift<clang::interp::Integral<32u, true> >(clang::interp::InterpState&, clang::interp::CodePtr, clang::interp::Integral<32u, true> const&, unsigned int)'
/usr/bin/ld: EvalEmitter.cpp:(.text._ZN5clang6interp11EvalEmitter7emitShlENS0_8PrimTypeES2_RKNS0_10SourceInfoE+0x2058): undefined reference to `bool clang::interp::CheckShift<clang::interp::Integral<32u, true> >(clang::interp::InterpState&, clang::interp::CodePtr, clang::interp::Integral<32u, true> const&, unsigned int)'

(etc)
2023-01-19 13:03:20 +01:00
Timm Bäder
2dbcfd298f Revert "[clang][Interp][NFC] Move CheckDivRem() implementation into Interp.cpp"
This reverts commit 9ee0d7494eb35f5addefcb730cdf5c002ddeacd2.
2023-01-19 12:37:20 +01:00
Timm Bäder
9ee0d7494e [clang][Interp][NFC] Move CheckDivRem() implementation into Interp.cpp
Just like we do with all the other Check* functions.
2023-01-19 12:18:34 +01:00
Timm Bäder
fddf6418e8 [clang][Interp][NFC] Remove shift error checking code duplication 2023-01-19 12:18:26 +01:00
Timm Bäder
6b0cd497b0 [clang][Interp] Check Field initialization after constructor call
Differential Revision: https://reviews.llvm.org/D136694
2023-01-19 09:42:22 +01:00
Timm Bäder
865094746e [clang][Interp] Track initialization state of local variables
Use an InlineDescriptor per local variable to track whether locals
have been initialized or not. This way we can support uninitialized
local variables in constexpr functions.

Differential Revision: https://reviews.llvm.org/D135750
2023-01-18 16:55:02 +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
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
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
6b3e5c595b [clang][Interp][NFC] Remove unused function 2022-11-07 09:42:41 +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
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
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
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
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
0e754cfadc [clang][Interp][NFC] Unify Call() implementations
The type parameter we used to pass to call() was unused. Use the same
implementation for void and value-returning function calls.
2022-10-22 10:32:05 +02:00
Timm Bäder
cc79ddb52c [clang][Interp] Check instance pointers before calling functions on them
Remove the double Call() implementation to reduce code duplication. Then
fix Function::getSource() so we can diagnose instance pointers being
null.

Differential Revision: https://reviews.llvm.org/D135513
2022-10-22 10:32:05 +02:00