-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcast_expr_fastinterp.cpp
More file actions
144 lines (130 loc) · 5.95 KB
/
cast_expr_fastinterp.cpp
File metadata and controls
144 lines (130 loc) · 5.95 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "cast_expr.h"
#include "fastinterp_ast_helper.hpp"
namespace PochiVM
{
// See comment in fastinterp_tpl_static_cast.cpp
//
constexpr uint64_t x_x86_64_uint64_to_double_magic_csts[3] = {
4985484788626358272ULL, 4841369599423283200ULL, 4985484787499139072ULL
};
void AstReinterpretCastExpr::FastInterpSetupSpillLocation()
{
thread_pochiVMContext->m_fastInterpStackFrameManager->ReserveTemp(m_operand->GetTypeId());
m_operand->FastInterpSetupSpillLocation();
}
static FastInterpBoilerplateInstance* WARN_UNUSED FIGetNoopBitCastButSpill(FISpillLocation spillLoc)
{
TestAssert(!spillLoc.IsNoSpill());
FastInterpBoilerplateInstance* inst = thread_pochiVMContext->m_fastInterpEngine->InstantiateBoilerplate(
FastInterpBoilerplateLibrary<FIPointerCastImpl>::SelectBoilerplateBluePrint(
true /*isNoopCast*/,
true /*spillOutput*/,
thread_pochiVMContext->m_fastInterpStackFrameManager->GetNumNoSpillIntegral(),
FIOpaqueParamsHelper::GetMaxOFP()));
spillLoc.PopulatePlaceholderIfSpill(inst, 0);
return inst;
}
FastInterpSnippet WARN_UNUSED AstReinterpretCastExpr::PrepareForFastInterp(FISpillLocation spillLoc)
{
TestAssert(thread_pochiVMContext->m_fastInterpStackFrameManager->CanReserveWithoutSpill(m_operand->GetTypeId()));
FastInterpSnippet snippet = m_operand->PrepareForFastInterp(x_FINoSpill);
TestAssert(!snippet.IsEmpty() && !snippet.IsUncontinuable());
if (spillLoc.IsNoSpill())
{
// this is a no-op, since reinterpret_cast is a no-op at bit-level
// TODO: FIXME: casting int to float changes the register location..
//
return snippet;
}
else
{
FastInterpBoilerplateInstance* inst = FIGetNoopBitCastButSpill(spillLoc);
return snippet.AddContinuation(inst);
}
}
void AstStaticCastExpr::FastInterpSetupSpillLocation()
{
thread_pochiVMContext->m_fastInterpStackFrameManager->ReserveTemp(m_operand->GetTypeId());
m_operand->FastInterpSetupSpillLocation();
}
FastInterpSnippet WARN_UNUSED AstStaticCastExpr::PrepareForFastInterp(FISpillLocation spillLoc)
{
TestAssert(thread_pochiVMContext->m_fastInterpStackFrameManager->CanReserveWithoutSpill(m_operand->GetTypeId()));
FastInterpSnippet snippet = m_operand->PrepareForFastInterp(x_FINoSpill);
TestAssert(!snippet.IsEmpty() && !snippet.IsUncontinuable());
TypeId srcType = m_operand->GetTypeId();
TypeId dstType = GetTypeId();
TestAssert(!srcType.IsVoid() && !dstType.IsVoid());
TestAssertIff(srcType.IsPointerType(), dstType.IsPointerType());
if (!srcType.IsPointerType())
{
// Case 1: static_cast between non-pointer types
// FIStaticCastImpl / FIStaticCastU64DoubleImpl
//
TestAssertImp(srcType.IsFloatingPoint(), dstType.IsFloatingPoint());
FastInterpBoilerplateInstance* inst;
// Special case: uint64_t -> double
// We currently need magic constant table support for uint64_t -> double cast,
// see comment in fastinterp_tpl_static_cast.cpp
//
//
if (srcType == TypeId::Get<uint64_t>() && dstType == TypeId::Get<double>())
{
inst = thread_pochiVMContext->m_fastInterpEngine->InstantiateBoilerplate(
FastInterpBoilerplateLibrary<FIStaticCastU64DoubleImpl>::SelectBoilerplateBluePrint(
!spillLoc.IsNoSpill(),
thread_pochiVMContext->m_fastInterpStackFrameManager->GetNumNoSpillIntegral(),
thread_pochiVMContext->m_fastInterpStackFrameManager->GetNumNoSpillFloat()));
spillLoc.PopulatePlaceholderIfSpill(inst, 0);
inst->PopulateConstantPlaceholder<uintptr_t>(1, reinterpret_cast<uintptr_t>(x_x86_64_uint64_to_double_magic_csts));
}
else
{
inst = thread_pochiVMContext->m_fastInterpEngine->InstantiateBoilerplate(
FastInterpBoilerplateLibrary<FIStaticCastImpl>::SelectBoilerplateBluePrint(
srcType.GetDefaultFastInterpTypeId(),
dstType.GetDefaultFastInterpTypeId(),
!spillLoc.IsNoSpill(),
thread_pochiVMContext->m_fastInterpStackFrameManager->GetNumNoSpillIntegral(),
thread_pochiVMContext->m_fastInterpStackFrameManager->GetNumNoSpillFloat()));
spillLoc.PopulatePlaceholderIfSpill(inst, 0);
}
return snippet.AddContinuation(inst);
}
else
{
// Case 2: static_cast between pointer types
// FIPointerCastImpl
//
ssize_t offset = AstTypeHelper::get_static_cast_offset(srcType, dstType);
if (offset == 0)
{
if (spillLoc.IsNoSpill())
{
// This cast is a no-op, since it doesn't change the pointer value, and does not spill the result
//
return snippet;
}
else
{
FastInterpBoilerplateInstance* inst = FIGetNoopBitCastButSpill(spillLoc);
return snippet.AddContinuation(inst);
}
}
else
{
// A pointer-cast with offset
//
FastInterpBoilerplateInstance* inst = thread_pochiVMContext->m_fastInterpEngine->InstantiateBoilerplate(
FastInterpBoilerplateLibrary<FIPointerCastImpl>::SelectBoilerplateBluePrint(
false /*isNoopCast*/,
!spillLoc.IsNoSpill(),
thread_pochiVMContext->m_fastInterpStackFrameManager->GetNumNoSpillIntegral(),
FIOpaqueParamsHelper::GetMaxOFP()));
spillLoc.PopulatePlaceholderIfSpill(inst, 0);
inst->PopulateConstantPlaceholder<uint64_t>(1, static_cast<uint64_t>(offset));
return snippet.AddContinuation(inst);
}
}
}
} // namespace PochiVM