Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[clang][Interp] Implement function calls
Add Call() and CallVoid() ops and use them to call functions. Only FunctionDecls are supported for now. Differential Revision: https://reviews.llvm.org/D132286
- Loading branch information
Showing
10 changed files
with
191 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify %s | ||
// RUN: %clang_cc1 -verify=ref %s | ||
|
||
// expected-no-diagnostics | ||
// ref-no-diagnostics | ||
|
||
constexpr void doNothing() {} | ||
constexpr int gimme5() { | ||
doNothing(); | ||
return 5; | ||
} | ||
static_assert(gimme5() == 5, ""); | ||
|
||
|
||
template<typename T> constexpr T identity(T t) { return t; } | ||
static_assert(identity(true), ""); | ||
static_assert(identity(true), ""); /// Compiled bytecode should be cached | ||
static_assert(!identity(false), ""); | ||
|
||
constexpr auto add(int a, int b) -> int { | ||
return identity(a) + identity(b); | ||
} | ||
|
||
constexpr int sub(int a, int b) { | ||
return a - b; | ||
} | ||
static_assert(sub(5, 2) == 3, ""); | ||
static_assert(sub(0, 5) == -5, ""); | ||
|
||
constexpr int norm(int n) { | ||
if (n >= 0) { | ||
return identity(n); | ||
} | ||
return -identity(n); | ||
} | ||
static_assert(norm(5) == norm(-5), ""); | ||
|
||
constexpr int square(int n) { | ||
return norm(n) * norm(n); | ||
} | ||
static_assert(square(2) == 4, ""); | ||
|
||
constexpr int add_second(int a, int b, bool doAdd = true) { | ||
if (doAdd) | ||
return a + b; | ||
return a; | ||
} | ||
static_assert(add_second(10, 3, true) == 13, ""); | ||
static_assert(add_second(10, 3) == 13, ""); | ||
static_assert(add_second(300, -20, false) == 300, ""); | ||
|
||
|
||
constexpr int sub(int a, int b, int c) { | ||
return a - b - c; | ||
} | ||
static_assert(sub(10, 8, 2) == 0, ""); | ||
|
||
|
||
constexpr int recursion(int i) { | ||
doNothing(); | ||
i = i - 1; | ||
if (i == 0) | ||
return identity(0); | ||
|
||
return recursion(i); | ||
} | ||
static_assert(recursion(10) == 0, ""); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters