-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest_fibonacci.cpp
68 lines (51 loc) · 1.95 KB
/
test_fibonacci.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "gtest/gtest.h"
#include "pochivm.h"
#include "codegen_context.hpp"
#include "test_util_helper.h"
using namespace PochiVM;
TEST(Sanity, FibonacciSeq)
{
AutoThreadPochiVMContext apv;
AutoThreadErrorContext arc;
AutoThreadLLVMCodegenContext alc;
thread_pochiVMContext->m_curModule = new AstModule("test");
using FnPrototype = uint64_t(*)(int) noexcept;
auto [fn, n] = NewFunction<FnPrototype>("fib_nth", "n");
fn.SetBody(
If(n <= Literal<int>(2)).Then(
Return(Literal<uint64_t>(1))
).Else(
Return(Call<FnPrototype>("fib_nth", n - Literal<int>(1))
+ Call<FnPrototype>("fib_nth", n - Literal<int>(2)))
)
);
ReleaseAssert(thread_pochiVMContext->m_curModule->Validate());
thread_pochiVMContext->m_curModule->PrepareForDebugInterp();
{
auto interpFn = thread_pochiVMContext->m_curModule->
GetDebugInterpGeneratedFunction<FnPrototype>("fib_nth");
uint64_t ret = interpFn(20);
ReleaseAssert(ret == 6765);
}
thread_pochiVMContext->m_curModule->PrepareForFastInterp();
{
FastInterpFunction<FnPrototype> interpFn = thread_pochiVMContext->m_curModule->
GetFastInterpGeneratedFunction<FnPrototype>("fib_nth");
uint64_t ret = interpFn(20);
ReleaseAssert(ret == 6765);
}
thread_pochiVMContext->m_curModule->EmitIR();
std::string _dst;
llvm::raw_string_ostream rso(_dst /*target*/);
thread_pochiVMContext->m_curModule->GetBuiltLLVMModule()->print(rso, nullptr);
std::string& dump = rso.str();
AssertIsExpectedOutput(dump);
thread_pochiVMContext->m_curModule->OptimizeIRIfNotDebugMode(2 /*optLevel*/);
SimpleJIT jit;
jit.SetModule(thread_pochiVMContext->m_curModule);
{
FnPrototype jitFn = jit.GetFunction<FnPrototype>("fib_nth");
uint64_t ret = jitFn(20);
ReleaseAssert(ret == 6765);
}
}