/
WasmSignature.cpp
220 lines (193 loc) · 6.55 KB
/
WasmSignature.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft Corporation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "WasmReaderPch.h"
#ifdef ENABLE_WASM
namespace Wasm
{
WasmSignature::WasmSignature() :
m_resultType(WasmTypes::Void),
m_id(Js::Constants::UninitializedValue),
m_paramSize(Js::Constants::InvalidArgSlot),
m_params(nullptr),
m_paramsCount(0),
m_shortSig(Js::Constants::InvalidSignature)
{
}
void WasmSignature::AllocateParams(Js::ArgSlot count, Recycler * recycler)
{
if (count > 0)
{
m_params = RecyclerNewArrayLeafZ(recycler, Local, count);
}
m_paramsCount = count;
}
void WasmSignature::SetParam(WasmTypes::WasmType type, Js::ArgSlot index)
{
if (index >= GetParamCount())
{
throw WasmCompilationException(_u("Parameter %u out of range (max %u)"), index, GetParamCount());
}
m_params[index] = Local(type);
}
void WasmSignature::SetResultType(WasmTypes::WasmType type)
{
Assert(m_resultType == WasmTypes::Void);
m_resultType = type;
}
void WasmSignature::SetSignatureId(uint32 id)
{
Assert(m_id == Js::Constants::UninitializedValue);
m_id = id;
}
Local WasmSignature::GetParam(Js::ArgSlot index) const
{
if (index >= GetParamCount())
{
throw WasmCompilationException(_u("Parameter %u out of range (max %u)"), index, GetParamCount());
}
return m_params[index];
}
WasmTypes::WasmType WasmSignature::GetResultType() const
{
return m_resultType;
}
Js::ArgSlot WasmSignature::GetParamCount() const
{
return m_paramsCount;
}
uint32 WasmSignature::GetSignatureId() const
{
return m_id;
}
size_t WasmSignature::GetShortSig() const
{
return m_shortSig;
}
bool WasmSignature::IsEquivalent(const WasmSignature* sig) const
{
if (m_shortSig != Js::Constants::InvalidSignature)
{
return sig->GetShortSig() == m_shortSig;
}
if (GetResultType() == sig->GetResultType() &&
GetParamCount() == sig->GetParamCount() &&
GetParamsSize() == sig->GetParamsSize())
{
return GetParamCount() == 0 || memcmp(m_params, sig->m_params, GetParamCount() * sizeof(Local)) == 0;
}
return false;
}
Js::ArgSlot WasmSignature::GetParamSize(Js::ArgSlot index) const
{
switch (GetParam(index))
{
case WasmTypes::F32:
case WasmTypes::I32:
CompileAssert(sizeof(float) == sizeof(int32));
#ifdef _M_X64
// on x64, we always alloc (at least) 8 bytes per arguments
return sizeof(void*);
#elif _M_IX86
return sizeof(int32);
#else
Assert(UNREACHED);
#endif
break;
case WasmTypes::F64:
case WasmTypes::I64:
CompileAssert(sizeof(double) == sizeof(int64));
return sizeof(int64);
break;
#define SIMD_CASE(TYPE, BASE) case WasmTypes::##TYPE:
FOREACH_SIMD_TYPE(SIMD_CASE)
#undef SIMD_CASE
CompileAssert(sizeof(Simd::simdvec) == 16);
return sizeof(Simd::simdvec);
break;
default:
throw WasmCompilationException(_u("Invalid param type"));
}
}
void WasmSignature::FinalizeSignature()
{
Assert(m_paramSize == Js::Constants::InvalidArgSlot);
Assert(m_shortSig == Js::Constants::InvalidSignature);
const Js::ArgSlot paramCount = GetParamCount();
m_paramSize = 0;
for (Js::ArgSlot i = 0; i < paramCount; ++i)
{
if (ArgSlotMath::Add(m_paramSize, GetParamSize(i), &m_paramSize))
{
throw WasmCompilationException(_u("Argument size too big"));
}
}
CompileAssert(Local::Void == 0);
#if 0
CompileAssert(Local::Limit - 1 <= 4);
// 3 bits for result type, 2 for each arg
// we don't need to reserve a sentinel bit because there is no result type with value of 7
uint32 sigSize = ((uint32)paramCount) * 2 + 3;
if (sigSize <= sizeof(m_shortSig) << 3)
{
m_shortSig = (m_shortSig << 3) | m_resultType;
for (Js::ArgSlot i = 0; i < paramCount; ++i)
{
// we can use 2 bits per arg by dropping void
m_shortSig = (m_shortSig << 2) | (m_params[i] - 1);
}
}
#endif
}
Js::ArgSlot WasmSignature::GetParamsSize() const
{
return m_paramSize;
}
WasmSignature* WasmSignature::FromIDL(WasmSignatureIDL* sig)
{
// must update WasmSignatureIDL when changing WasmSignature
CompileAssert(sizeof(Wasm::WasmSignature) == sizeof(WasmSignatureIDL));
CompileAssert(offsetof(Wasm::WasmSignature, m_resultType) == offsetof(WasmSignatureIDL, resultType));
CompileAssert(offsetof(Wasm::WasmSignature, m_id) == offsetof(WasmSignatureIDL, id));
CompileAssert(offsetof(Wasm::WasmSignature, m_paramSize) == offsetof(WasmSignatureIDL, paramSize));
CompileAssert(offsetof(Wasm::WasmSignature, m_paramsCount) == offsetof(WasmSignatureIDL, paramsCount));
CompileAssert(offsetof(Wasm::WasmSignature, m_params) == offsetof(WasmSignatureIDL, params));
CompileAssert(offsetof(Wasm::WasmSignature, m_shortSig) == offsetof(WasmSignatureIDL, shortSig));
CompileAssert(sizeof(Local) == sizeof(int));
return reinterpret_cast<WasmSignature*>(sig);
}
uint32 WasmSignature::WriteSignatureToString(_Out_writes_(maxlen) char16* out, uint32 maxlen)
{
AssertOrFailFast(out != nullptr);
uint32 numwritten = 0;
numwritten += _snwprintf_s(out+numwritten, maxlen-numwritten, _TRUNCATE, _u("("));
for (Js::ArgSlot i = 0; i < this->GetParamCount(); i++)
{
if (i != 0)
{
numwritten += _snwprintf_s(out+numwritten, maxlen-numwritten, _TRUNCATE, _u(", "));
}
numwritten += _snwprintf_s(out+numwritten, maxlen-numwritten, _TRUNCATE, _u("%ls"), WasmTypes::GetTypeName(this->GetParam(i)));
}
if (numwritten >= maxlen-12) {
// null out the last 12 characters so we can properly end it
for (int i = 1; i <= 12; i++) {
*(out + maxlen - i) = 0;
}
numwritten -= 12;
numwritten += _snwprintf_s(out + numwritten, maxlen - numwritten, _TRUNCATE, _u("..."));
}
numwritten += _snwprintf_s(out + numwritten, maxlen - numwritten, _TRUNCATE, _u(")->%ls"), WasmTypes::GetTypeName(this->GetResultType()));
return numwritten;
}
void WasmSignature::Dump(uint32 maxlen)
{
#ifdef ENABLE_DEBUG_CONFIG_OPTIONS
char16 buf[512] = { 0 };
this->WriteSignatureToString(buf, min(maxlen, 512u));
Output::Print(buf);
#endif
}
} // namespace Wasm
#endif // ENABLE_WASM