| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* |
| 4 |
* Copyright 2014 Mozilla Foundation |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
* you may not use this file except in compliance with the License. |
| 8 |
* You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "wasm/AsmJS.h" |
| 20 |
|
| 21 |
#include "mozilla/Attributes.h" |
| 22 |
#include "mozilla/Compression.h" |
| 23 |
#include "mozilla/MathAlgorithms.h" |
| 24 |
#include "mozilla/Maybe.h" |
| 25 |
#include "mozilla/ScopeExit.h" |
| 26 |
#include "mozilla/Sprintf.h" // SprintfLiteral |
| 27 |
#include "mozilla/Try.h" // MOZ_TRY* |
| 28 |
#include "mozilla/Utf8.h" // mozilla::Utf8Unit |
| 29 |
#include "mozilla/Variant.h" |
| 30 |
|
| 31 |
#include <algorithm> |
| 32 |
#include <new> |
| 33 |
|
| 34 |
#include "jsmath.h" |
| 35 |
|
| 36 |
#include "frontend/BytecodeCompiler.h" // CompileStandaloneFunction |
| 37 |
#include "frontend/FrontendContext.h" // js::FrontendContext |
| 38 |
#include "frontend/FunctionSyntaxKind.h" // FunctionSyntaxKind |
| 39 |
#include "frontend/ParseNode.h" |
| 40 |
#include "frontend/Parser-macros.h" // MOZ_TRY_* |
| 41 |
#include "frontend/Parser.h" |
| 42 |
#include "frontend/ParserAtom.h" // ParserAtomsTable, TaggedParserAtomIndex |
| 43 |
#include "frontend/SharedContext.h" // TopLevelFunction |
| 44 |
#include "frontend/TaggedParserAtomIndexHasher.h" // TaggedParserAtomIndexHasher |
| 45 |
#include "gc/GC.h" |
| 46 |
#include "gc/Policy.h" |
| 47 |
#include "jit/InlinableNatives.h" |
| 48 |
#include "js/BuildId.h" // JS::BuildIdCharVector |
| 49 |
#include "js/experimental/JitInfo.h" |
| 50 |
#include "js/friend/ErrorMessages.h" // JSMSG_* |
| 51 |
#include "js/MemoryMetrics.h" |
| 52 |
#include "js/Printf.h" |
| 53 |
#include "js/ScalarType.h" // js::Scalar::Type |
| 54 |
#include "js/SourceText.h" |
| 55 |
#include "js/StableStringChars.h" |
| 56 |
#include "js/Wrapper.h" |
| 57 |
#include "util/DifferentialTesting.h" |
| 58 |
#include "util/StringBuilder.h" |
| 59 |
#include "util/Text.h" |
| 60 |
#include "vm/ErrorReporting.h" |
| 61 |
#include "vm/FunctionFlags.h" // js::FunctionFlags |
| 62 |
#include "vm/GeneratorAndAsyncKind.h" // js::GeneratorKind, js::FunctionAsyncKind |
| 63 |
#include "vm/Interpreter.h" |
| 64 |
#include "vm/SelfHosting.h" |
| 65 |
#include "vm/Time.h" |
| 66 |
#include "vm/TypedArrayObject.h" |
| 67 |
#include "vm/Warnings.h" // js::WarnNumberASCII |
| 68 |
#include "wasm/WasmCompile.h" |
| 69 |
#include "wasm/WasmFeatures.h" |
| 70 |
#include "wasm/WasmGenerator.h" |
| 71 |
#include "wasm/WasmInstance.h" |
| 72 |
#include "wasm/WasmIonCompile.h" |
| 73 |
#include "wasm/WasmJS.h" |
| 74 |
#include "wasm/WasmModuleTypes.h" |
| 75 |
#include "wasm/WasmSerialize.h" |
| 76 |
#include "wasm/WasmSignalHandlers.h" |
| 77 |
#include "wasm/WasmValidate.h" |
| 78 |
|
| 79 |
#include "frontend/SharedContext-inl.h" |
| 80 |
#include "vm/ArrayBufferObject-inl.h" |
| 81 |
#include "vm/JSObject-inl.h" |
| 82 |
#include "wasm/WasmInstance-inl.h" |
| 83 |
|
| 84 |
using namespace js; |
| 85 |
using namespace js::frontend; |
| 86 |
using namespace js::jit; |
| 87 |
using namespace js::wasm; |
| 88 |
|
| 89 |
using JS::AsmJSOption; |
| 90 |
using JS::AutoStableStringChars; |
| 91 |
using JS::GenericNaN; |
| 92 |
using JS::SourceText; |
| 93 |
using mozilla::Abs; |
| 94 |
using mozilla::AsVariant; |
| 95 |
using mozilla::CeilingLog2; |
| 96 |
using mozilla::HashGeneric; |
| 97 |
using mozilla::IsNegativeZero; |
| 98 |
using mozilla::IsPositiveZero; |
| 99 |
using mozilla::IsPowerOfTwo; |
| 100 |
using mozilla::Maybe; |
| 101 |
using mozilla::Nothing; |
| 102 |
using mozilla::PodZero; |
| 103 |
using mozilla::PositiveInfinity; |
| 104 |
using mozilla::Some; |
| 105 |
using mozilla::Utf8Unit; |
| 106 |
using mozilla::Compression::LZ4; |
| 107 |
|
| 108 |
using FunctionVector = JS::GCVector<JSFunction*>; |
| 109 |
|
| 110 |
/*****************************************************************************/ |
| 111 |
|
| 112 |
// A wasm module can either use no memory, a unshared memory (ArrayBuffer) or |
| 113 |
// shared memory (SharedArrayBuffer). |
| 114 |
|
| 115 |
enum class MemoryUsage { None = false, Unshared = 1, Shared = 2 }; |
| 116 |
|
| 117 |
// The asm.js valid heap lengths are precisely the WASM valid heap lengths for |
| 118 |
// ARM greater or equal to MinHeapLength |
| 119 |
static const size_t MinHeapLength = PageSize; |
| 120 |
// An asm.js heap can in principle be up to INT32_MAX bytes but requirements |
| 121 |
// on the format restrict it further to the largest pseudo-ARM-immediate. |
| 122 |
// See IsValidAsmJSHeapLength(). |
| 123 |
static const uint64_t MaxHeapLength = 0x7f000000; |
| 124 |
|
| 125 |
static uint64_t RoundUpToNextValidAsmJSHeapLength(uint64_t length) { |
| 126 |
if (length <= MinHeapLength) { |
| 127 |
return MinHeapLength; |
| 128 |
} |
| 129 |
|
| 130 |
return wasm::RoundUpToNextValidARMImmediate(length); |
| 131 |
} |
| 132 |
|
| 133 |
static uint64_t DivideRoundingUp(uint64_t a, uint64_t b) { |
| 134 |
return (a + (b - 1)) / b; |
| 135 |
} |
| 136 |
|
| 137 |
/*****************************************************************************/ |
| 138 |
// asm.js module object |
| 139 |
|
| 140 |
// The asm.js spec recognizes this set of builtin Math functions. |
| 141 |
enum AsmJSMathBuiltinFunction { |
| 142 |
AsmJSMathBuiltin_sin, |
| 143 |
AsmJSMathBuiltin_cos, |
| 144 |
AsmJSMathBuiltin_tan, |
| 145 |
AsmJSMathBuiltin_asin, |
| 146 |
AsmJSMathBuiltin_acos, |
| 147 |
AsmJSMathBuiltin_atan, |
| 148 |
AsmJSMathBuiltin_ceil, |
| 149 |
AsmJSMathBuiltin_floor, |
| 150 |
AsmJSMathBuiltin_exp, |
| 151 |
AsmJSMathBuiltin_log, |
| 152 |
AsmJSMathBuiltin_pow, |
| 153 |
AsmJSMathBuiltin_sqrt, |
| 154 |
AsmJSMathBuiltin_abs, |
| 155 |
AsmJSMathBuiltin_atan2, |
| 156 |
AsmJSMathBuiltin_imul, |
| 157 |
AsmJSMathBuiltin_fround, |
| 158 |
AsmJSMathBuiltin_min, |
| 159 |
AsmJSMathBuiltin_max, |
| 160 |
AsmJSMathBuiltin_clz32 |
| 161 |
}; |
| 162 |
|
| 163 |
// LitValPOD is a restricted version of LitVal suitable for asm.js that is |
| 164 |
// always POD. |
| 165 |
|
| 166 |
struct LitValPOD { |
| 167 |
PackedTypeCode valType_; |
| 168 |
union U { |
| 169 |
uint32_t u32_; |
| 170 |
uint64_t u64_; |
| 171 |
float f32_; |
| 172 |
double f64_; |
| 173 |
} u; |
| 174 |
|
| 175 |
LitValPOD() = default; |
| 176 |
|
| 177 |
explicit LitValPOD(uint32_t u32) : valType_(ValType(ValType::I32).packed()) { |
| 178 |
u.u32_ = u32; |
| 179 |
} |
| 180 |
explicit LitValPOD(uint64_t u64) : valType_(ValType(ValType::I64).packed()) { |
| 181 |
u.u64_ = u64; |
| 182 |
} |
| 183 |
|
| 184 |
explicit LitValPOD(float f32) : valType_(ValType(ValType::F32).packed()) { |
| 185 |
u.f32_ = f32; |
| 186 |
} |
| 187 |
explicit LitValPOD(double f64) : valType_(ValType(ValType::F64).packed()) { |
| 188 |
u.f64_ = f64; |
| 189 |
} |
| 190 |
|
| 191 |
LitVal asLitVal() const { |
| 192 |
switch (valType_.typeCode()) { |
| 193 |
case TypeCode::I32: |
| 194 |
return LitVal(u.u32_); |
| 195 |
case TypeCode::I64: |
| 196 |
return LitVal(u.u64_); |
| 197 |
case TypeCode::F32: |
| 198 |
return LitVal(u.f32_); |
| 199 |
case TypeCode::F64: |
| 200 |
return LitVal(u.f64_); |
| 201 |
default: |
| 202 |
MOZ_CRASH("Can't happen"); |
| 203 |
} |
| 204 |
} |
| 205 |
}; |
| 206 |
|
| 207 |
static_assert(std::is_pod_v<LitValPOD>, |
| 208 |
"must be POD to be simply serialized/deserialized"); |
| 209 |
|
| 210 |
// An AsmJSGlobal represents a JS global variable in the asm.js module function. |
| 211 |
class AsmJSGlobal { |
| 212 |
public: |
| 213 |
enum Which { |
| 214 |
Variable, |
| 215 |
FFI, |
| 216 |
ArrayView, |
| 217 |
ArrayViewCtor, |
| 218 |
MathBuiltinFunction, |
| 219 |
Constant |
| 220 |
}; |
| 221 |
enum VarInitKind { InitConstant, InitImport }; |
| 222 |
enum ConstantKind { GlobalConstant, MathConstant }; |
| 223 |
|
| 224 |
private: |
| 225 |
struct CacheablePod { |
| 226 |
Which which_; |
| 227 |
union V { |
| 228 |
struct { |
| 229 |
VarInitKind initKind_; |
| 230 |
union U { |
| 231 |
PackedTypeCode importValType_; |
| 232 |
LitValPOD val_; |
| 233 |
} u; |
| 234 |
} var; |
| 235 |
uint32_t ffiIndex_; |
| 236 |
Scalar::Type viewType_; |
| 237 |
AsmJSMathBuiltinFunction mathBuiltinFunc_; |
| 238 |
struct { |
| 239 |
ConstantKind kind_; |
| 240 |
double value_; |
| 241 |
} constant; |
| 242 |
} u; |
| 243 |
} pod; |
| 244 |
CacheableChars field_; |
| 245 |
|
| 246 |
friend class ModuleValidatorShared; |
| 247 |
template <typename Unit> |
| 248 |
friend class ModuleValidator; |
| 249 |
|
| 250 |
public: |
| 251 |
AsmJSGlobal() = default; |
| 252 |
AsmJSGlobal(Which which, UniqueChars field) { |
| 253 |
mozilla::PodZero(&pod); // zero padding for Valgrind |
| 254 |
pod.which_ = which; |
| 255 |
field_ = std::move(field); |
| 256 |
} |
| 257 |
const char* field() const { return field_.get(); } |
| 258 |
Which which() const { return pod.which_; } |
| 259 |
VarInitKind varInitKind() const { |
| 260 |
MOZ_ASSERT(pod.which_ == Variable); |
| 261 |
return pod.u.var.initKind_; |
| 262 |
} |
| 263 |
LitValPOD varInitVal() const { |
| 264 |
MOZ_ASSERT(pod.which_ == Variable); |
| 265 |
MOZ_ASSERT(pod.u.var.initKind_ == InitConstant); |
| 266 |
return pod.u.var.u.val_; |
| 267 |
} |
| 268 |
ValType varInitImportType() const { |
| 269 |
MOZ_ASSERT(pod.which_ == Variable); |
| 270 |
MOZ_ASSERT(pod.u.var.initKind_ == InitImport); |
| 271 |
return ValType(pod.u.var.u.importValType_); |
| 272 |
} |
| 273 |
uint32_t ffiIndex() const { |
| 274 |
MOZ_ASSERT(pod.which_ == FFI); |
| 275 |
return pod.u.ffiIndex_; |
| 276 |
} |
| 277 |
// When a view is created from an imported constructor: |
| 278 |
// var I32 = stdlib.Int32Array; |
| 279 |
// var i32 = new I32(buffer); |
| 280 |
// the second import has nothing to validate and thus has a null field. |
| 281 |
Scalar::Type viewType() const { |
| 282 |
MOZ_ASSERT(pod.which_ == ArrayView || pod.which_ == ArrayViewCtor); |
| 283 |
return pod.u.viewType_; |
| 284 |
} |
| 285 |
AsmJSMathBuiltinFunction mathBuiltinFunction() const { |
| 286 |
MOZ_ASSERT(pod.which_ == MathBuiltinFunction); |
| 287 |
return pod.u.mathBuiltinFunc_; |
| 288 |
} |
| 289 |
ConstantKind constantKind() const { |
| 290 |
MOZ_ASSERT(pod.which_ == Constant); |
| 291 |
return pod.u.constant.kind_; |
| 292 |
} |
| 293 |
double constantValue() const { |
| 294 |
MOZ_ASSERT(pod.which_ == Constant); |
| 295 |
return pod.u.constant.value_; |
| 296 |
} |
| 297 |
}; |
| 298 |
|
| 299 |
using AsmJSGlobalVector = Vector<AsmJSGlobal, 0, SystemAllocPolicy>; |
| 300 |
|
| 301 |
// An AsmJSImport is slightly different than an asm.js FFI function: a single |
| 302 |
// asm.js FFI function can be called with many different signatures. When |
| 303 |
// compiled to wasm, each unique FFI function paired with signature generates a |
| 304 |
// wasm import. |
| 305 |
class AsmJSImport { |
| 306 |
uint32_t ffiIndex_; |
| 307 |
|
| 308 |
public: |
| 309 |
AsmJSImport() = default; |
| 310 |
explicit AsmJSImport(uint32_t ffiIndex) : ffiIndex_(ffiIndex) {} |
| 311 |
uint32_t ffiIndex() const { return ffiIndex_; } |
| 312 |
}; |
| 313 |
|
| 314 |
using AsmJSImportVector = Vector<AsmJSImport, 0, SystemAllocPolicy>; |
| 315 |
|
| 316 |
// An AsmJSExport logically extends Export with the extra information needed for |
| 317 |
// an asm.js exported function, viz., the offsets in module's source chars in |
| 318 |
// case the function is toString()ed. |
| 319 |
class AsmJSExport { |
| 320 |
uint32_t funcIndex_ = 0; |
| 321 |
|
| 322 |
// All fields are treated as cacheable POD: |
| 323 |
uint32_t startOffsetInModule_ = 0; // Store module-start-relative offsets |
| 324 |
uint32_t endOffsetInModule_ = 0; // so preserved by serialization. |
| 325 |
|
| 326 |
public: |
| 327 |
AsmJSExport() = default; |
| 328 |
AsmJSExport(uint32_t funcIndex, uint32_t startOffsetInModule, |
| 329 |
uint32_t endOffsetInModule) |
| 330 |
: funcIndex_(funcIndex), |
| 331 |
startOffsetInModule_(startOffsetInModule), |
| 332 |
endOffsetInModule_(endOffsetInModule) {} |
| 333 |
uint32_t funcIndex() const { return funcIndex_; } |
| 334 |
uint32_t startOffsetInModule() const { return startOffsetInModule_; } |
| 335 |
uint32_t endOffsetInModule() const { return endOffsetInModule_; } |
| 336 |
}; |
| 337 |
|
| 338 |
using AsmJSExportVector = Vector<AsmJSExport, 0, SystemAllocPolicy>; |
| 339 |
|
| 340 |
// Holds the immutable guts of an AsmJSModule. |
| 341 |
// |
| 342 |
// CodeMetadataForAsmJSImpl is built incrementally by ModuleValidator and then |
| 343 |
// shared immutably between AsmJSModules. |
| 344 |
|
| 345 |
struct js::CodeMetadataForAsmJSImpl : CodeMetadataForAsmJS { |
| 346 |
uint32_t numFFIs = 0; |
| 347 |
uint32_t srcLength = 0; |
| 348 |
uint32_t srcLengthWithRightBrace = 0; |
| 349 |
AsmJSGlobalVector asmJSGlobals; |
| 350 |
AsmJSImportVector asmJSImports; |
| 351 |
AsmJSExportVector asmJSExports; |
| 352 |
CacheableCharsVector asmJSFuncNames; |
| 353 |
CacheableChars globalArgumentName; |
| 354 |
CacheableChars importArgumentName; |
| 355 |
CacheableChars bufferArgumentName; |
| 356 |
|
| 357 |
// These values are not serialized since they are relative to the |
| 358 |
// containing script which can be different between serialization and |
| 359 |
// deserialization contexts. Thus, they must be set explicitly using the |
| 360 |
// ambient Parser/ScriptSource after deserialization. |
| 361 |
// |
| 362 |
// srcStart refers to the offset in the ScriptSource to the beginning of |
| 363 |
// the asm.js module function. If the function has been created with the |
| 364 |
// Function constructor, this will be the first character in the function |
| 365 |
// source. Otherwise, it will be the opening parenthesis of the arguments |
| 366 |
// list. |
| 367 |
uint32_t toStringStart; |
| 368 |
uint32_t srcStart; |
| 369 |
bool strict; |
| 370 |
bool alwaysUseFdlibm = false; |
| 371 |
RefPtr<ScriptSource> source; |
| 372 |
|
| 373 |
uint32_t srcEndBeforeCurly() const { return srcStart + srcLength; } |
| 374 |
uint32_t srcEndAfterCurly() const { |
| 375 |
return srcStart + srcLengthWithRightBrace; |
| 376 |
} |
| 377 |
|
| 378 |
CodeMetadataForAsmJSImpl() : toStringStart(0), srcStart(0), strict(false) {} |
| 379 |
~CodeMetadataForAsmJSImpl() = default; |
| 380 |
|
| 381 |
const CodeMetadataForAsmJSImpl& asAsmJS() const { return *this; } |
| 382 |
|
| 383 |
const AsmJSExport& lookupAsmJSExport(uint32_t funcIndex) const { |
| 384 |
// The AsmJSExportVector isn't stored in sorted order so do a linear |
| 385 |
// search. This is for the super-cold and already-expensive toString() |
| 386 |
// path and the number of exports is generally small. |
| 387 |
for (const AsmJSExport& exp : asmJSExports) { |
| 388 |
if (exp.funcIndex() == funcIndex) { |
| 389 |
return exp; |
| 390 |
} |
| 391 |
} |
| 392 |
MOZ_CRASH("missing asm.js func export"); |
| 393 |
} |
| 394 |
|
| 395 |
bool mutedErrors() const { return source->mutedErrors(); } |
| 396 |
const char16_t* displayURL() const { |
| 397 |
return source->hasDisplayURL() ? source->displayURL() : nullptr; |
| 398 |
} |
| 399 |
ScriptSource* maybeScriptSource() const { return source.get(); } |
| 400 |
bool getFuncNameForAsmJS(uint32_t funcIndex, UTF8Bytes* name) const { |
| 401 |
const char* p = asmJSFuncNames[funcIndex].get(); |
| 402 |
if (!p) { |
| 403 |
return true; |
| 404 |
} |
| 405 |
return name->append(p, strlen(p)); |
| 406 |
} |
| 407 |
|
| 408 |
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const { |
| 409 |
return asmJSGlobals.sizeOfExcludingThis(mallocSizeOf) + |
| 410 |
asmJSImports.sizeOfExcludingThis(mallocSizeOf) + |
| 411 |
asmJSExports.sizeOfExcludingThis(mallocSizeOf) + |
| 412 |
asmJSFuncNames.sizeOfExcludingThis(mallocSizeOf) + |
| 413 |
globalArgumentName.sizeOfExcludingThis(mallocSizeOf) + |
| 414 |
importArgumentName.sizeOfExcludingThis(mallocSizeOf) + |
| 415 |
bufferArgumentName.sizeOfExcludingThis(mallocSizeOf); |
| 416 |
} |
| 417 |
}; |
| 418 |
|
| 419 |
using MutableCodeMetadataForAsmJSImpl = RefPtr<CodeMetadataForAsmJSImpl>; |
| 420 |
|
| 421 |
/*****************************************************************************/ |
| 422 |
// ParseNode utilities |
| 423 |
|
| 424 |
static inline ParseNode* NextNode(ParseNode* pn) { return pn->pn_next; } |
| 425 |
|
| 426 |
static inline ParseNode* UnaryKid(ParseNode* pn) { |
| 427 |
return pn->as<UnaryNode>().kid(); |
| 428 |
} |
| 429 |
|
| 430 |
static inline ParseNode* BinaryRight(ParseNode* pn) { |
| 431 |
return pn->as<BinaryNode>().right(); |
| 432 |
} |
| 433 |
|
| 434 |
static inline ParseNode* BinaryLeft(ParseNode* pn) { |
| 435 |
return pn->as<BinaryNode>().left(); |
| 436 |
} |
| 437 |
|
| 438 |
static inline ParseNode* ReturnExpr(ParseNode* pn) { |
| 439 |
MOZ_ASSERT(pn->isKind(ParseNodeKind::ReturnStmt)); |
| 440 |
return UnaryKid(pn); |
| 441 |
} |
| 442 |
|
| 443 |
static inline ParseNode* TernaryKid1(ParseNode* pn) { |
| 444 |
return pn->as<TernaryNode>().kid1(); |
| 445 |
} |
| 446 |
|
| 447 |
static inline ParseNode* TernaryKid2(ParseNode* pn) { |
| 448 |
return pn->as<TernaryNode>().kid2(); |
| 449 |
} |
| 450 |
|
| 451 |
static inline ParseNode* TernaryKid3(ParseNode* pn) { |
| 452 |
return pn->as<TernaryNode>().kid3(); |
| 453 |
} |
| 454 |
|
| 455 |
static inline ParseNode* ListHead(ParseNode* pn) { |
| 456 |
return pn->as<ListNode>().head(); |
| 457 |
} |
| 458 |
|
| 459 |
static inline unsigned ListLength(ParseNode* pn) { |
| 460 |
return pn->as<ListNode>().count(); |
| 461 |
} |
| 462 |
|
| 463 |
static inline ParseNode* CallCallee(ParseNode* pn) { |
| 464 |
MOZ_ASSERT(pn->isKind(ParseNodeKind::CallExpr)); |
| 465 |
return BinaryLeft(pn); |
| 466 |
} |
| 467 |
|
| 468 |
static inline unsigned CallArgListLength(ParseNode* pn) { |
| 469 |
MOZ_ASSERT(pn->isKind(ParseNodeKind::CallExpr)); |
| 470 |
return ListLength(BinaryRight(pn)); |
| 471 |
} |
| 472 |
|
| 473 |
static inline ParseNode* CallArgList(ParseNode* pn) { |
| 474 |
MOZ_ASSERT(pn->isKind(ParseNodeKind::CallExpr)); |
| 475 |
return ListHead(BinaryRight(pn)); |
| 476 |
} |
| 477 |
|
| 478 |
static inline ParseNode* VarListHead(ParseNode* pn) { |
| 479 |
MOZ_ASSERT(pn->isKind(ParseNodeKind::VarStmt) || |
| 480 |
pn->isKind(ParseNodeKind::ConstDecl)); |
| 481 |
return ListHead(pn); |
| 482 |
} |
| 483 |
|
| 484 |
static inline bool IsDefaultCase(ParseNode* pn) { |
| 485 |
return pn->as<CaseClause>().isDefault(); |
| 486 |
} |
| 487 |
|
| 488 |
static inline ParseNode* CaseExpr(ParseNode* pn) { |
| 489 |
return pn->as<CaseClause>().caseExpression(); |
| 490 |
} |
| 491 |
|
| 492 |
static inline ParseNode* CaseBody(ParseNode* pn) { |
| 493 |
return pn->as<CaseClause>().statementList(); |
| 494 |
} |
| 495 |
|
| 496 |
static inline ParseNode* BinaryOpLeft(ParseNode* pn) { |
| 497 |
MOZ_ASSERT(pn->isBinaryOperation()); |
| 498 |
MOZ_ASSERT(pn->as<ListNode>().count() == 2); |
| 499 |
return ListHead(pn); |
| 500 |
} |
| 501 |
|
| 502 |
static inline ParseNode* BinaryOpRight(ParseNode* pn) { |
| 503 |
MOZ_ASSERT(pn->isBinaryOperation()); |
| 504 |
MOZ_ASSERT(pn->as<ListNode>().count() == 2); |
| 505 |
return NextNode(ListHead(pn)); |
| 506 |
} |
| 507 |
|
| 508 |
static inline ParseNode* BitwiseLeft(ParseNode* pn) { return BinaryOpLeft(pn); } |
| 509 |
|
| 510 |
static inline ParseNode* BitwiseRight(ParseNode* pn) { |
| 511 |
return BinaryOpRight(pn); |
| 512 |
} |
| 513 |
|
| 514 |
static inline ParseNode* MultiplyLeft(ParseNode* pn) { |
| 515 |
MOZ_ASSERT(pn->isKind(ParseNodeKind::MulExpr)); |
| 516 |
return BinaryOpLeft(pn); |
| 517 |
} |
| 518 |
|
| 519 |
static inline ParseNode* MultiplyRight(ParseNode* pn) { |
| 520 |
MOZ_ASSERT(pn->isKind(ParseNodeKind::MulExpr)); |
| 521 |
return BinaryOpRight(pn); |
| 522 |
} |
| 523 |
|
| 524 |
static inline ParseNode* AddSubLeft(ParseNode* pn) { |
| 525 |
MOZ_ASSERT(pn->isKind(ParseNodeKind::AddExpr) || |
| 526 |
pn->isKind(ParseNodeKind::SubExpr)); |
| 527 |
return BinaryOpLeft(pn); |
| 528 |
} |
| 529 |
|
| 530 |
static inline ParseNode* AddSubRight(ParseNode* pn) { |
| 531 |
MOZ_ASSERT(pn->isKind(ParseNodeKind::AddExpr) || |
| 532 |
pn->isKind(ParseNodeKind::SubExpr)); |
| 533 |
return BinaryOpRight(pn); |
| 534 |
} |
| 535 |
|
| 536 |
static inline ParseNode* DivOrModLeft(ParseNode* pn) { |
| 537 |
MOZ_ASSERT(pn->isKind(ParseNodeKind::DivExpr) || |
| 538 |
pn->isKind(ParseNodeKind::ModExpr)); |
| 539 |
return BinaryOpLeft(pn); |
| 540 |
} |
| 541 |
|
| 542 |
static inline ParseNode* DivOrModRight(ParseNode* pn) { |
| 543 |
MOZ_ASSERT(pn->isKind(ParseNodeKind::DivExpr) || |
| 544 |
pn->isKind(ParseNodeKind::ModExpr)); |
| 545 |
return BinaryOpRight(pn); |
| 546 |
} |
| 547 |
|
| 548 |
static inline ParseNode* ComparisonLeft(ParseNode* pn) { |
| 549 |
return BinaryOpLeft(pn); |
| 550 |
} |
| 551 |
|
| 552 |
static inline ParseNode* ComparisonRight(ParseNode* pn) { |
| 553 |
return BinaryOpRight(pn); |
| 554 |
} |
| 555 |
|
| 556 |
static inline bool IsExpressionStatement(ParseNode* pn) { |
| 557 |
return pn->isKind(ParseNodeKind::ExpressionStmt); |
| 558 |
} |
| 559 |
|
| 560 |
static inline ParseNode* ExpressionStatementExpr(ParseNode* pn) { |
| 561 |
MOZ_ASSERT(pn->isKind(ParseNodeKind::ExpressionStmt)); |
| 562 |
return UnaryKid(pn); |
| 563 |
} |
| 564 |
|
| 565 |
static inline TaggedParserAtomIndex LoopControlMaybeLabel(ParseNode* pn) { |
| 566 |
MOZ_ASSERT(pn->isKind(ParseNodeKind::BreakStmt) || |
| 567 |
pn->isKind(ParseNodeKind::ContinueStmt)); |
| 568 |
return pn->as<LoopControlStatement>().label(); |
| 569 |
} |
| 570 |
|
| 571 |
static inline TaggedParserAtomIndex LabeledStatementLabel(ParseNode* pn) { |
| 572 |
return pn->as<LabeledStatement>().label(); |
| 573 |
} |
| 574 |
|
| 575 |
static inline ParseNode* LabeledStatementStatement(ParseNode* pn) { |
| 576 |
return pn->as<LabeledStatement>().statement(); |
| 577 |
} |
| 578 |
|
| 579 |
static double NumberNodeValue(ParseNode* pn) { |
| 580 |
return pn->as<NumericLiteral>().value(); |
| 581 |
} |
| 582 |
|
| 583 |
static bool NumberNodeHasFrac(ParseNode* pn) { |
| 584 |
return pn->as<NumericLiteral>().decimalPoint() == HasDecimal; |
| 585 |
} |
| 586 |
|
| 587 |
static ParseNode* DotBase(ParseNode* pn) { |
| 588 |
return &pn->as<PropertyAccess>().expression(); |
| 589 |
} |
| 590 |
|
| 591 |
static TaggedParserAtomIndex DotMember(ParseNode* pn) { |
| 592 |
return pn->as<PropertyAccess>().name(); |
| 593 |
} |
| 594 |
|
| 595 |
static ParseNode* ElemBase(ParseNode* pn) { |
| 596 |
return &pn->as<PropertyByValue>().expression(); |
| 597 |
} |
| 598 |
|
| 599 |
static ParseNode* ElemIndex(ParseNode* pn) { |
| 600 |
return &pn->as<PropertyByValue>().key(); |
| 601 |
} |
| 602 |
|
| 603 |
static inline TaggedParserAtomIndex FunctionName(FunctionNode* funNode) { |
| 604 |
if (auto name = funNode->funbox()->explicitName()) { |
| 605 |
return name; |
| 606 |
} |
| 607 |
return TaggedParserAtomIndex::null(); |
| 608 |
} |
| 609 |
|
| 610 |
static inline ParseNode* FunctionFormalParametersList(FunctionNode* fn, |
| 611 |
unsigned* numFormals) { |
| 612 |
ParamsBodyNode* argsBody = fn->body(); |
| 613 |
|
| 614 |
// The number of formals is equal to the number of parameters (excluding the |
| 615 |
// trailing lexical scope). There are no destructuring or rest parameters for |
| 616 |
// asm.js functions. |
| 617 |
*numFormals = argsBody->count(); |
| 618 |
|
| 619 |
// If the function has been fully parsed, the trailing function body node is a |
| 620 |
// lexical scope. If we've only parsed the function parameters, the last node |
| 621 |
// is the last parameter. |
| 622 |
if (*numFormals > 0 && argsBody->last()->is<LexicalScopeNode>()) { |
| 623 |
MOZ_ASSERT(argsBody->last()->as<LexicalScopeNode>().scopeBody()->isKind( |
| 624 |
ParseNodeKind::StatementList)); |
| 625 |
(*numFormals)--; |
| 626 |
} |
| 627 |
|
| 628 |
return argsBody->head(); |
| 629 |
} |
| 630 |
|
| 631 |
static inline ParseNode* FunctionStatementList(FunctionNode* funNode) { |
| 632 |
LexicalScopeNode* last = funNode->body()->body(); |
| 633 |
MOZ_ASSERT(last->isEmptyScope()); |
| 634 |
ParseNode* body = last->scopeBody(); |
| 635 |
MOZ_ASSERT(body->isKind(ParseNodeKind::StatementList)); |
| 636 |
return body; |
| 637 |
} |
| 638 |
|
| 639 |
static inline bool IsNormalObjectField(ParseNode* pn) { |
| 640 |
return pn->isKind(ParseNodeKind::PropertyDefinition) && |
| 641 |
pn->as<PropertyDefinition>().accessorType() == AccessorType::None && |
| 642 |
BinaryLeft(pn)->isKind(ParseNodeKind::ObjectPropertyName); |
| 643 |
} |
| 644 |
|
| 645 |
static inline TaggedParserAtomIndex ObjectNormalFieldName(ParseNode* pn) { |
| 646 |
MOZ_ASSERT(IsNormalObjectField(pn)); |
| 647 |
MOZ_ASSERT(BinaryLeft(pn)->isKind(ParseNodeKind::ObjectPropertyName)); |
| 648 |
return BinaryLeft(pn)->as<NameNode>().atom(); |
| 649 |
} |
| 650 |
|
| 651 |
static inline ParseNode* ObjectNormalFieldInitializer(ParseNode* pn) { |
| 652 |
MOZ_ASSERT(IsNormalObjectField(pn)); |
| 653 |
return BinaryRight(pn); |
| 654 |
} |
| 655 |
|
| 656 |
static inline bool IsUseOfName(ParseNode* pn, TaggedParserAtomIndex name) { |
| 657 |
return pn->isName(name); |
| 658 |
} |
| 659 |
|
| 660 |
static inline bool IsIgnoredDirectiveName(TaggedParserAtomIndex atom) { |
| 661 |
return atom != TaggedParserAtomIndex::WellKnown::use_strict_(); |
| 662 |
} |
| 663 |
|
| 664 |
static inline bool IsIgnoredDirective(ParseNode* pn) { |
| 665 |
return pn->isKind(ParseNodeKind::ExpressionStmt) && |
| 666 |
UnaryKid(pn)->isKind(ParseNodeKind::StringExpr) && |
| 667 |
IsIgnoredDirectiveName(UnaryKid(pn)->as<NameNode>().atom()); |
| 668 |
} |
| 669 |
|
| 670 |
static inline bool IsEmptyStatement(ParseNode* pn) { |
| 671 |
return pn->isKind(ParseNodeKind::EmptyStmt); |
| 672 |
} |
| 673 |
|
| 674 |
static inline ParseNode* SkipEmptyStatements(ParseNode* pn) { |
| 675 |
while (pn && IsEmptyStatement(pn)) { |
| 676 |
pn = pn->pn_next; |
| 677 |
} |
| 678 |
return pn; |
| 679 |
} |
| 680 |
|
| 681 |
static inline ParseNode* NextNonEmptyStatement(ParseNode* pn) { |
| 682 |
return SkipEmptyStatements(pn->pn_next); |
| 683 |
} |
| 684 |
|
| 685 |
template <typename Unit> |
| 686 |
static bool GetToken(AsmJSParser<Unit>& parser, TokenKind* tkp) { |
| 687 |
auto& ts = parser.tokenStream; |
| 688 |
TokenKind tk; |
| 689 |
while (true) { |
| 690 |
if (!ts.getToken(&tk, TokenStreamShared::SlashIsRegExp)) { |
| 691 |
return false; |
| 692 |
} |
| 693 |
if (tk != TokenKind::Semi) { |
| 694 |
break; |
| 695 |
} |
| 696 |
} |
| 697 |
*tkp = tk; |
| 698 |
return true; |
| 699 |
} |
| 700 |
|
| 701 |
template <typename Unit> |
| 702 |
static bool PeekToken(AsmJSParser<Unit>& parser, TokenKind* tkp) { |
| 703 |
auto& ts = parser.tokenStream; |
| 704 |
TokenKind tk; |
| 705 |
while (true) { |
| 706 |
if (!ts.peekToken(&tk, TokenStream::SlashIsRegExp)) { |
| 707 |
return false; |
| 708 |
} |
| 709 |
if (tk != TokenKind::Semi) { |
| 710 |
break; |
| 711 |
} |
| 712 |
ts.consumeKnownToken(TokenKind::Semi, TokenStreamShared::SlashIsRegExp); |
| 713 |
} |
| 714 |
*tkp = tk; |
| 715 |
return true; |
| 716 |
} |
| 717 |
|
| 718 |
template <typename Unit> |
| 719 |
static bool ParseVarOrConstStatement(AsmJSParser<Unit>& parser, |
| 720 |
ParseNode** var) { |
| 721 |
TokenKind tk; |
| 722 |
if (!PeekToken(parser, &tk)) { |
| 723 |
return false; |
| 724 |
} |
| 725 |
if (tk != TokenKind::Var && tk != TokenKind::Const) { |
| 726 |
*var = nullptr; |
| 727 |
return true; |
| 728 |
} |
| 729 |
|
| 730 |
MOZ_TRY_VAR_OR_RETURN(*var, parser.statementListItem(YieldIsName), false); |
| 731 |
|
| 732 |
MOZ_ASSERT((*var)->isKind(ParseNodeKind::VarStmt) || |
| 733 |
(*var)->isKind(ParseNodeKind::ConstDecl)); |
| 734 |
return true; |
| 735 |
} |
| 736 |
|
| 737 |
/*****************************************************************************/ |
| 738 |
|
| 739 |
// Represents the type and value of an asm.js numeric literal. |
| 740 |
// |
| 741 |
// A literal is a double iff the literal contains a decimal point (even if the |
| 742 |
// fractional part is 0). Otherwise, integers may be classified: |
| 743 |
// fixnum: [0, 2^31) |
| 744 |
// negative int: [-2^31, 0) |
| 745 |
// big unsigned: [2^31, 2^32) |
| 746 |
// out of range: otherwise |
| 747 |
// Lastly, a literal may be a float literal which is any double or integer |
| 748 |
// literal coerced with Math.fround. |
| 749 |
class NumLit { |
| 750 |
public: |
| 751 |
enum Which { |
| 752 |
Fixnum, |
| 753 |
NegativeInt, |
| 754 |
BigUnsigned, |
| 755 |
Double, |
| 756 |
Float, |
| 757 |
OutOfRangeInt = -1 |
| 758 |
}; |
| 759 |
|
| 760 |
private: |
| 761 |
Which which_; |
| 762 |
JS::Value value_; |
| 763 |
|
| 764 |
public: |
| 765 |
NumLit() = default; |
| 766 |
|
| 767 |
NumLit(Which w, const Value& v) : which_(w), value_(v) {} |
| 768 |
|
| 769 |
Which which() const { return which_; } |
| 770 |
|
| 771 |
int32_t toInt32() const { |
| 772 |
MOZ_ASSERT(which_ == Fixnum || which_ == NegativeInt || |
| 773 |
which_ == BigUnsigned); |
| 774 |
return value_.toInt32(); |
| 775 |
} |
| 776 |
|
| 777 |
uint32_t toUint32() const { return (uint32_t)toInt32(); } |
| 778 |
|
| 779 |
double toDouble() const { |
| 780 |
MOZ_ASSERT(which_ == Double); |
| 781 |
return value_.toDouble(); |
| 782 |
} |
| 783 |
|
| 784 |
float toFloat() const { |
| 785 |
MOZ_ASSERT(which_ == Float); |
| 786 |
return float(value_.toDouble()); |
| 787 |
} |
| 788 |
|
| 789 |
Value scalarValue() const { |
| 790 |
MOZ_ASSERT(which_ != OutOfRangeInt); |
| 791 |
return value_; |
| 792 |
} |
| 793 |
|
| 794 |
bool valid() const { return which_ != OutOfRangeInt; } |
| 795 |
|
| 796 |
bool isZeroBits() const { |
| 797 |
MOZ_ASSERT(valid()); |
| 798 |
switch (which()) { |
| 799 |
case NumLit::Fixnum: |
| 800 |
case NumLit::NegativeInt: |
| 801 |
case NumLit::BigUnsigned: |
| 802 |
return toInt32() == 0; |
| 803 |
case NumLit::Double: |
| 804 |
return IsPositiveZero(toDouble()); |
| 805 |
case NumLit::Float: |
| 806 |
return IsPositiveZero(toFloat()); |
| 807 |
case NumLit::OutOfRangeInt: |
| 808 |
MOZ_CRASH("can't be here because of valid() check above"); |
| 809 |
} |
| 810 |
return false; |
| 811 |
} |
| 812 |
|
| 813 |
LitValPOD value() const { |
| 814 |
switch (which_) { |
| 815 |
case NumLit::Fixnum: |
| 816 |
case NumLit::NegativeInt: |
| 817 |
case NumLit::BigUnsigned: |
| 818 |
return LitValPOD(toUint32()); |
| 819 |
case NumLit::Float: |
| 820 |
return LitValPOD(toFloat()); |
| 821 |
case NumLit::Double: |
| 822 |
return LitValPOD(toDouble()); |
| 823 |
case NumLit::OutOfRangeInt:; |
| 824 |
} |
| 825 |
MOZ_CRASH("bad literal"); |
| 826 |
} |
| 827 |
}; |
| 828 |
|
| 829 |
// Represents the type of a general asm.js expression. |
| 830 |
// |
| 831 |
// A canonical subset of types representing the coercion targets: Int, Float, |
| 832 |
// Double. |
| 833 |
// |
| 834 |
// Void is also part of the canonical subset. |
| 835 |
|
| 836 |
class Type { |
| 837 |
public: |
| 838 |
enum Which { |
| 839 |
Fixnum = NumLit::Fixnum, |
| 840 |
Signed = NumLit::NegativeInt, |
| 841 |
Unsigned = NumLit::BigUnsigned, |
| 842 |
DoubleLit = NumLit::Double, |
| 843 |
Float = NumLit::Float, |
| 844 |
Double, |
| 845 |
MaybeDouble, |
| 846 |
MaybeFloat, |
| 847 |
Floatish, |
| 848 |
Int, |
| 849 |
Intish, |
| 850 |
Void |
| 851 |
}; |
| 852 |
|
| 853 |
private: |
| 854 |
Which which_; |
| 855 |
|
| 856 |
public: |
| 857 |
Type() = default; |
| 858 |
MOZ_IMPLICIT Type(Which w) : which_(w) {} |
| 859 |
|
| 860 |
// Map an already canonicalized Type to the return type of a function call. |
| 861 |
static Type ret(Type t) { |
| 862 |
MOZ_ASSERT(t.isCanonical()); |
| 863 |
// The 32-bit external type is Signed, not Int. |
| 864 |
return t.isInt() ? Signed : t; |
| 865 |
} |
| 866 |
|
| 867 |
static Type lit(const NumLit& lit) { |
| 868 |
MOZ_ASSERT(lit.valid()); |
| 869 |
Which which = Type::Which(lit.which()); |
| 870 |
MOZ_ASSERT(which >= Fixnum && which <= Float); |
| 871 |
Type t; |
| 872 |
t.which_ = which; |
| 873 |
return t; |
| 874 |
} |
| 875 |
|
| 876 |
// Map |t| to one of the canonical vartype representations of a |
| 877 |
// wasm::ValType. |
| 878 |
static Type canonicalize(Type t) { |
| 879 |
switch (t.which()) { |
| 880 |
case Fixnum: |
| 881 |
case Signed: |
| 882 |
case Unsigned: |
| 883 |
case Int: |
| 884 |
return Int; |
| 885 |
|
| 886 |
case Float: |
| 887 |
return Float; |
| 888 |
|
| 889 |
case DoubleLit: |
| 890 |
case Double: |
| 891 |
return Double; |
| 892 |
|
| 893 |
case Void: |
| 894 |
return Void; |
| 895 |
|
| 896 |
case MaybeDouble: |
| 897 |
case MaybeFloat: |
| 898 |
case Floatish: |
| 899 |
case Intish: |
| 900 |
// These types need some kind of coercion, they can't be mapped |
| 901 |
// to an VarType. |
| 902 |
break; |
| 903 |
} |
| 904 |
MOZ_CRASH("Invalid vartype"); |
| 905 |
} |
| 906 |
|
| 907 |
Which which() const { return which_; } |
| 908 |
|
| 909 |
bool operator==(Type rhs) const { return which_ == rhs.which_; } |
| 910 |
bool operator!=(Type rhs) const { return which_ != rhs.which_; } |
| 911 |
|
| 912 |
bool operator<=(Type rhs) const { |
| 913 |
switch (rhs.which_) { |
| 914 |
case Signed: |
| 915 |
return isSigned(); |
| 916 |
case Unsigned: |
| 917 |
return isUnsigned(); |
| 918 |
case DoubleLit: |
| 919 |
return isDoubleLit(); |
| 920 |
case Double: |
| 921 |
return isDouble(); |
| 922 |
case Float: |
| 923 |
return isFloat(); |
| 924 |
case MaybeDouble: |
| 925 |
return isMaybeDouble(); |
| 926 |
case MaybeFloat: |
| 927 |
return isMaybeFloat(); |
| 928 |
case Floatish: |
| 929 |
return isFloatish(); |
| 930 |
case Int: |
| 931 |
return isInt(); |
| 932 |
case Intish: |
| 933 |
return isIntish(); |
| 934 |
case Fixnum: |
| 935 |
return isFixnum(); |
| 936 |
case Void: |
| 937 |
return isVoid(); |
| 938 |
} |
| 939 |
MOZ_CRASH("unexpected rhs type"); |
| 940 |
} |
| 941 |
|
| 942 |
bool isFixnum() const { return which_ == Fixnum; } |
| 943 |
|
| 944 |
bool isSigned() const { return which_ == Signed || which_ == Fixnum; } |
| 945 |
|
| 946 |
bool isUnsigned() const { return which_ == Unsigned || which_ == Fixnum; } |
| 947 |
|
| 948 |
bool isInt() const { return isSigned() || isUnsigned() || which_ == Int; } |
| 949 |
|
| 950 |
bool isIntish() const { return isInt() || which_ == Intish; } |
| 951 |
|
| 952 |
bool isDoubleLit() const { return which_ == DoubleLit; } |
| 953 |
|
| 954 |
bool isDouble() const { return isDoubleLit() || which_ == Double; } |
| 955 |
|
| 956 |
bool isMaybeDouble() const { return isDouble() || which_ == MaybeDouble; } |
| 957 |
|
| 958 |
bool isFloat() const { return which_ == Float; } |
| 959 |
|
| 960 |
bool isMaybeFloat() const { return isFloat() || which_ == MaybeFloat; } |
| 961 |
|
| 962 |
bool isFloatish() const { return isMaybeFloat() || which_ == Floatish; } |
| 963 |
|
| 964 |
bool isVoid() const { return which_ == Void; } |
| 965 |
|
| 966 |
bool isExtern() const { return isDouble() || isSigned(); } |
| 967 |
|
| 968 |
// Check if this is one of the valid types for a function argument. |
| 969 |
bool isArgType() const { return isInt() || isFloat() || isDouble(); } |
| 970 |
|
| 971 |
// Check if this is one of the valid types for a function return value. |
| 972 |
bool isReturnType() const { |
| 973 |
return isSigned() || isFloat() || isDouble() || isVoid(); |
| 974 |
} |
| 975 |
|
| 976 |
// Check if this is one of the valid types for a global variable. |
| 977 |
bool isGlobalVarType() const { return isArgType(); } |
| 978 |
|
| 979 |
// Check if this is one of the canonical vartype representations of a |
| 980 |
// wasm::ValType, or is void. See Type::canonicalize(). |
| 981 |
bool isCanonical() const { |
| 982 |
switch (which()) { |
| 983 |
case Int: |
| 984 |
case Float: |
| 985 |
case Double: |
| 986 |
case Void: |
| 987 |
return true; |
| 988 |
default: |
| 989 |
return false; |
| 990 |
} |
| 991 |
} |
| 992 |
|
| 993 |
// Check if this is a canonical representation of a wasm::ValType. |
| 994 |
bool isCanonicalValType() const { return !isVoid() && isCanonical(); } |
| 995 |
|
| 996 |
// Convert this canonical type to a wasm::ValType. |
| 997 |
ValType canonicalToValType() const { |
| 998 |
switch (which()) { |
| 999 |
case Int: |
| 1000 |
return ValType::I32; |
| 1001 |
case Float: |
| 1002 |
return ValType::F32; |
| 1003 |
case Double: |
| 1004 |
return ValType::F64; |
| 1005 |
default: |
| 1006 |
MOZ_CRASH("Need canonical type"); |
| 1007 |
} |
| 1008 |
} |
| 1009 |
|
| 1010 |
Maybe<ValType> canonicalToReturnType() const { |
| 1011 |
return isVoid() ? Nothing() : Some(canonicalToValType()); |
| 1012 |
} |
| 1013 |
|
| 1014 |
// Convert this type to a wasm::TypeCode for use in a wasm |
| 1015 |
// block signature. This works for all types, including non-canonical |
| 1016 |
// ones. Consequently, the type isn't valid for subsequent asm.js |
| 1017 |
// validation; it's only valid for use in producing wasm. |
| 1018 |
TypeCode toWasmBlockSignatureType() const { |
| 1019 |
switch (which()) { |
| 1020 |
case Fixnum: |
| 1021 |
case Signed: |
| 1022 |
case Unsigned: |
| 1023 |
case Int: |
| 1024 |
case Intish: |
| 1025 |
return TypeCode::I32; |
| 1026 |
|
| 1027 |
case Float: |
| 1028 |
case MaybeFloat: |
| 1029 |
case Floatish: |
| 1030 |
return TypeCode::F32; |
| 1031 |
|
| 1032 |
case DoubleLit: |
| 1033 |
case Double: |
| 1034 |
case MaybeDouble: |
| 1035 |
return TypeCode::F64; |
| 1036 |
|
| 1037 |
case Void: |
| 1038 |
return TypeCode::BlockVoid; |
| 1039 |
} |
| 1040 |
MOZ_CRASH("Invalid Type"); |
| 1041 |
} |
| 1042 |
|
| 1043 |
const char* toChars() const { |
| 1044 |
switch (which_) { |
| 1045 |
case Double: |
| 1046 |
return "double"; |
| 1047 |
case DoubleLit: |
| 1048 |
return "doublelit"; |
| 1049 |
case MaybeDouble: |
| 1050 |
return "double?"; |
| 1051 |
case Float: |
| 1052 |
return "float"; |
| 1053 |
case Floatish: |
| 1054 |
return "floatish"; |
| 1055 |
case MaybeFloat: |
| 1056 |
return "float?"; |
| 1057 |
case Fixnum: |
| 1058 |
return "fixnum"; |
| 1059 |
case Int: |
| 1060 |
return "int"; |
| 1061 |
case Signed: |
| 1062 |
return "signed"; |
| 1063 |
case Unsigned: |
| 1064 |
return "unsigned"; |
| 1065 |
case Intish: |
| 1066 |
return "intish"; |
| 1067 |
case Void: |
| 1068 |
return "void"; |
| 1069 |
} |
| 1070 |
MOZ_CRASH("Invalid Type"); |
| 1071 |
} |
| 1072 |
}; |
| 1073 |
|
| 1074 |
static const unsigned VALIDATION_LIFO_DEFAULT_CHUNK_SIZE = 4 * 1024; |
| 1075 |
|
| 1076 |
class MOZ_STACK_CLASS ModuleValidatorShared { |
| 1077 |
public: |
| 1078 |
struct Memory { |
| 1079 |
MemoryUsage usage; |
| 1080 |
uint64_t minLength; |
| 1081 |
|
| 1082 |
uint64_t minPages() const { return DivideRoundingUp(minLength, PageSize); } |
| 1083 |
|
| 1084 |
Memory() = default; |
| 1085 |
}; |
| 1086 |
|
| 1087 |
class Func { |
| 1088 |
TaggedParserAtomIndex name_; |
| 1089 |
uint32_t sigIndex_; |
| 1090 |
uint32_t firstUse_; |
| 1091 |
uint32_t funcDefIndex_; |
| 1092 |
|
| 1093 |
bool defined_; |
| 1094 |
|
| 1095 |
// Available when defined: |
| 1096 |
uint32_t srcBegin_; |
| 1097 |
uint32_t srcEnd_; |
| 1098 |
uint32_t line_; |
| 1099 |
Bytes bytes_; |
| 1100 |
Uint32Vector callSiteLineNums_; |
| 1101 |
|
| 1102 |
public: |
| 1103 |
Func(TaggedParserAtomIndex name, uint32_t sigIndex, uint32_t firstUse, |
| 1104 |
uint32_t funcDefIndex) |
| 1105 |
: name_(name), |
| 1106 |
sigIndex_(sigIndex), |
| 1107 |
firstUse_(firstUse), |
| 1108 |
funcDefIndex_(funcDefIndex), |
| 1109 |
defined_(false), |
| 1110 |
srcBegin_(0), |
| 1111 |
srcEnd_(0), |
| 1112 |
line_(0) {} |
| 1113 |
|
| 1114 |
TaggedParserAtomIndex name() const { return name_; } |
| 1115 |
uint32_t sigIndex() const { return sigIndex_; } |
| 1116 |
uint32_t firstUse() const { return firstUse_; } |
| 1117 |
bool defined() const { return defined_; } |
| 1118 |
uint32_t funcDefIndex() const { return funcDefIndex_; } |
| 1119 |
|
| 1120 |
void define(ParseNode* fn, uint32_t line, Bytes&& bytes, |
| 1121 |
Uint32Vector&& callSiteLineNums) { |
| 1122 |
MOZ_ASSERT(!defined_); |
| 1123 |
defined_ = true; |
| 1124 |
srcBegin_ = fn->pn_pos.begin; |
| 1125 |
srcEnd_ = fn->pn_pos.end; |
| 1126 |
line_ = line; |
| 1127 |
bytes_ = std::move(bytes); |
| 1128 |
callSiteLineNums_ = std::move(callSiteLineNums); |
| 1129 |
} |
| 1130 |
|
| 1131 |
uint32_t srcBegin() const { |
| 1132 |
MOZ_ASSERT(defined_); |
| 1133 |
return srcBegin_; |
| 1134 |
} |
| 1135 |
uint32_t srcEnd() const { |
| 1136 |
MOZ_ASSERT(defined_); |
| 1137 |
return srcEnd_; |
| 1138 |
} |
| 1139 |
uint32_t line() const { |
| 1140 |
MOZ_ASSERT(defined_); |
| 1141 |
return line_; |
| 1142 |
} |
| 1143 |
const Bytes& bytes() const { |
| 1144 |
MOZ_ASSERT(defined_); |
| 1145 |
return bytes_; |
| 1146 |
} |
| 1147 |
Uint32Vector& callSiteLineNums() { |
| 1148 |
MOZ_ASSERT(defined_); |
| 1149 |
return callSiteLineNums_; |
| 1150 |
} |
| 1151 |
}; |
| 1152 |
|
| 1153 |
using ConstFuncVector = Vector<const Func*>; |
| 1154 |
using FuncVector = Vector<Func>; |
| 1155 |
|
| 1156 |
class Table { |
| 1157 |
uint32_t sigIndex_; |
| 1158 |
TaggedParserAtomIndex name_; |
| 1159 |
uint32_t firstUse_; |
| 1160 |
uint32_t mask_; |
| 1161 |
bool defined_; |
| 1162 |
|
| 1163 |
public: |
| 1164 |
Table(uint32_t sigIndex, TaggedParserAtomIndex name, uint32_t firstUse, |
| 1165 |
uint32_t mask) |
| 1166 |
: sigIndex_(sigIndex), |
| 1167 |
name_(name), |
| 1168 |
firstUse_(firstUse), |
| 1169 |
mask_(mask), |
| 1170 |
defined_(false) {} |
| 1171 |
|
| 1172 |
Table(Table&& rhs) = delete; |
| 1173 |
|
| 1174 |
uint32_t sigIndex() const { return sigIndex_; } |
| 1175 |
TaggedParserAtomIndex name() const { return name_; } |
| 1176 |
uint32_t firstUse() const { return firstUse_; } |
| 1177 |
unsigned mask() const { return mask_; } |
| 1178 |
bool defined() const { return defined_; } |
| 1179 |
void define() { |
| 1180 |
MOZ_ASSERT(!defined_); |
| 1181 |
defined_ = true; |
| 1182 |
} |
| 1183 |
}; |
| 1184 |
|
| 1185 |
using TableVector = Vector<Table*>; |
| 1186 |
|
| 1187 |
class Global { |
| 1188 |
public: |
| 1189 |
enum Which { |
| 1190 |
Variable, |
| 1191 |
ConstantLiteral, |
| 1192 |
ConstantImport, |
| 1193 |
Function, |
| 1194 |
Table, |
| 1195 |
FFI, |
| 1196 |
ArrayView, |
| 1197 |
ArrayViewCtor, |
| 1198 |
MathBuiltinFunction |
| 1199 |
}; |
| 1200 |
|
| 1201 |
private: |
| 1202 |
Which which_; |
| 1203 |
union U { |
| 1204 |
struct VarOrConst { |
| 1205 |
Type::Which type_; |
| 1206 |
unsigned index_; |
| 1207 |
NumLit literalValue_; |
| 1208 |
|
| 1209 |
VarOrConst(unsigned index, const NumLit& lit) |
| 1210 |
: type_(Type::lit(lit).which()), |
| 1211 |
index_(index), |
| 1212 |
literalValue_(lit) // copies |lit| |
| 1213 |
{} |
| 1214 |
|
| 1215 |
VarOrConst(unsigned index, Type::Which which) |
| 1216 |
: type_(which), index_(index) { |
| 1217 |
// The |literalValue_| field remains unused and |
| 1218 |
// uninitialized for non-constant variables. |
| 1219 |
} |
| 1220 |
|
| 1221 |
explicit VarOrConst(double constant) |
| 1222 |
: type_(Type::Double), |
| 1223 |
literalValue_(NumLit::Double, DoubleValue(constant)) { |
| 1224 |
// The index_ field is unused and uninitialized for |
| 1225 |
// constant doubles. |
| 1226 |
} |
| 1227 |
} varOrConst; |
| 1228 |
uint32_t funcDefIndex_; |
| 1229 |
uint32_t tableIndex_; |
| 1230 |
uint32_t ffiIndex_; |
| 1231 |
Scalar::Type viewType_; |
| 1232 |
AsmJSMathBuiltinFunction mathBuiltinFunc_; |
| 1233 |
|
| 1234 |
// |varOrConst|, through |varOrConst.literalValue_|, has a |
| 1235 |
// non-trivial constructor and therefore MUST be placement-new'd |
| 1236 |
// into existence. |
| 1237 |
MOZ_PUSH_DISABLE_NONTRIVIAL_UNION_WARNINGS |
| 1238 |
U() : funcDefIndex_(0) {} |
| 1239 |
MOZ_POP_DISABLE_NONTRIVIAL_UNION_WARNINGS |
| 1240 |
} u; |
| 1241 |
|
| 1242 |
friend class ModuleValidatorShared; |
| 1243 |
template <typename Unit> |
| 1244 |
friend class ModuleValidator; |
| 1245 |
friend class js::LifoAlloc; |
| 1246 |
|
| 1247 |
explicit Global(Which which) : which_(which) {} |
| 1248 |
|
| 1249 |
public: |
| 1250 |
Which which() const { return which_; } |
| 1251 |
Type varOrConstType() const { |
| 1252 |
MOZ_ASSERT(which_ == Variable || which_ == ConstantLiteral || |
| 1253 |
which_ == ConstantImport); |
| 1254 |
return u.varOrConst.type_; |
| 1255 |
} |
| 1256 |
unsigned varOrConstIndex() const { |
| 1257 |
MOZ_ASSERT(which_ == Variable || which_ == ConstantImport); |
| 1258 |
return u.varOrConst.index_; |
| 1259 |
} |
| 1260 |
bool isConst() const { |
| 1261 |
return which_ == ConstantLiteral || which_ == ConstantImport; |
| 1262 |
} |
| 1263 |
NumLit constLiteralValue() const { |
| 1264 |
MOZ_ASSERT(which_ == ConstantLiteral); |
| 1265 |
return u.varOrConst.literalValue_; |
| 1266 |
} |
| 1267 |
uint32_t funcDefIndex() const { |
| 1268 |
MOZ_ASSERT(which_ == Function); |
| 1269 |
return u.funcDefIndex_; |
| 1270 |
} |
| 1271 |
uint32_t tableIndex() const { |
| 1272 |
MOZ_ASSERT(which_ == Table); |
| 1273 |
return u.tableIndex_; |
| 1274 |
} |
| 1275 |
unsigned ffiIndex() const { |
| 1276 |
MOZ_ASSERT(which_ == FFI); |
| 1277 |
return u.ffiIndex_; |
| 1278 |
} |
| 1279 |
Scalar::Type viewType() const { |
| 1280 |
MOZ_ASSERT(which_ == ArrayView || which_ == ArrayViewCtor); |
| 1281 |
return u.viewType_; |
| 1282 |
} |
| 1283 |
bool isMathFunction() const { return which_ == MathBuiltinFunction; } |
| 1284 |
AsmJSMathBuiltinFunction mathBuiltinFunction() const { |
| 1285 |
MOZ_ASSERT(which_ == MathBuiltinFunction); |
| 1286 |
return u.mathBuiltinFunc_; |
| 1287 |
} |
| 1288 |
}; |
| 1289 |
|
| 1290 |
struct MathBuiltin { |
| 1291 |
enum Kind { Function, Constant }; |
| 1292 |
Kind kind; |
| 1293 |
|
| 1294 |
union { |
| 1295 |
double cst; |
| 1296 |
AsmJSMathBuiltinFunction func; |
| 1297 |
} u; |
| 1298 |
|
| 1299 |
MathBuiltin() : kind(Kind(-1)), u{} {} |
| 1300 |
explicit MathBuiltin(double cst) : kind(Constant) { u.cst = cst; } |
| 1301 |
explicit MathBuiltin(AsmJSMathBuiltinFunction func) : kind(Function) { |
| 1302 |
u.func = func; |
| 1303 |
} |
| 1304 |
}; |
| 1305 |
|
| 1306 |
struct ArrayView { |
| 1307 |
ArrayView(TaggedParserAtomIndex name, Scalar::Type type) |
| 1308 |
: name(name), type(type) {} |
| 1309 |
|
| 1310 |
TaggedParserAtomIndex name; |
| 1311 |
Scalar::Type type; |
| 1312 |
}; |
| 1313 |
|
| 1314 |
protected: |
| 1315 |
class HashableSig { |
| 1316 |
uint32_t sigIndex_; |
| 1317 |
const TypeContext& types_; |
| 1318 |
|
| 1319 |
public: |
| 1320 |
HashableSig(uint32_t sigIndex, const TypeContext& types) |
| 1321 |
: sigIndex_(sigIndex), types_(types) {} |
| 1322 |
uint32_t sigIndex() const { return sigIndex_; } |
| 1323 |
const FuncType& funcType() const { return types_[sigIndex_].funcType(); } |
| 1324 |
|
| 1325 |
// Implement HashPolicy: |
| 1326 |
using Lookup = const FuncType&; |
| 1327 |
static HashNumber hash(Lookup l) { return l.hash(nullptr); } |
| 1328 |
static bool match(HashableSig lhs, Lookup rhs) { |
| 1329 |
return FuncType::strictlyEquals(lhs.funcType(), rhs); |
| 1330 |
} |
| 1331 |
}; |
| 1332 |
|
| 1333 |
class NamedSig : public HashableSig { |
| 1334 |
TaggedParserAtomIndex name_; |
| 1335 |
|
| 1336 |
public: |
| 1337 |
NamedSig(TaggedParserAtomIndex name, uint32_t sigIndex, |
| 1338 |
const TypeContext& types) |
| 1339 |
: HashableSig(sigIndex, types), name_(name) {} |
| 1340 |
TaggedParserAtomIndex name() const { return name_; } |
| 1341 |
|
| 1342 |
// Implement HashPolicy: |
| 1343 |
struct Lookup { |
| 1344 |
TaggedParserAtomIndex name; |
| 1345 |
const FuncType& funcType; |
| 1346 |
Lookup(TaggedParserAtomIndex name, const FuncType& funcType) |
| 1347 |
: name(name), funcType(funcType) {} |
| 1348 |
}; |
| 1349 |
static HashNumber hash(Lookup l) { |
| 1350 |
return HashGeneric(TaggedParserAtomIndexHasher::hash(l.name), |
| 1351 |
l.funcType.hash(nullptr)); |
| 1352 |
} |
| 1353 |
static bool match(NamedSig lhs, Lookup rhs) { |
| 1354 |
return lhs.name() == rhs.name && |
| 1355 |
FuncType::strictlyEquals(lhs.funcType(), rhs.funcType); |
| 1356 |
} |
| 1357 |
}; |
| 1358 |
|
| 1359 |
using SigSet = HashSet<HashableSig, HashableSig>; |
| 1360 |
using FuncImportMap = HashMap<NamedSig, uint32_t, NamedSig>; |
| 1361 |
using GlobalMap = |
| 1362 |
HashMap<TaggedParserAtomIndex, Global*, TaggedParserAtomIndexHasher>; |
| 1363 |
using MathNameMap = |
| 1364 |
HashMap<TaggedParserAtomIndex, MathBuiltin, TaggedParserAtomIndexHasher>; |
| 1365 |
using ArrayViewVector = Vector<ArrayView>; |
| 1366 |
|
| 1367 |
protected: |
| 1368 |
FrontendContext* fc_; |
| 1369 |
ParserAtomsTable& parserAtoms_; |
| 1370 |
FunctionNode* moduleFunctionNode_; |
| 1371 |
TaggedParserAtomIndex moduleFunctionName_; |
| 1372 |
TaggedParserAtomIndex globalArgumentName_; |
| 1373 |
TaggedParserAtomIndex importArgumentName_; |
| 1374 |
TaggedParserAtomIndex bufferArgumentName_; |
| 1375 |
MathNameMap standardLibraryMathNames_; |
| 1376 |
|
| 1377 |
// Validation-internal state: |
| 1378 |
LifoAlloc validationLifo_; |
| 1379 |
Memory memory_; |
| 1380 |
FuncVector funcDefs_; |
| 1381 |
TableVector tables_; |
| 1382 |
GlobalMap globalMap_; |
| 1383 |
SigSet sigSet_; |
| 1384 |
FuncImportMap funcImportMap_; |
| 1385 |
ArrayViewVector arrayViews_; |
| 1386 |
|
| 1387 |
// State used to build the AsmJSModule in finish(): |
| 1388 |
CompilerEnvironment compilerEnv_; |
| 1389 |
MutableModuleMetadata moduleMeta_; |
| 1390 |
MutableCodeMetadata codeMeta_; |
| 1391 |
MutableCodeMetadataForAsmJSImpl codeMetaForAsmJS_; |
| 1392 |
|
| 1393 |
// Error reporting: |
| 1394 |
UniqueChars errorString_ = nullptr; |
| 1395 |
uint32_t errorOffset_ = UINT32_MAX; |
| 1396 |
bool errorOverRecursed_ = false; |
| 1397 |
|
| 1398 |
protected: |
| 1399 |
ModuleValidatorShared(FrontendContext* fc, ParserAtomsTable& parserAtoms, |
| 1400 |
MutableModuleMetadata moduleMeta, |
| 1401 |
MutableCodeMetadata codeMeta, |
| 1402 |
FunctionNode* moduleFunctionNode) |
| 1403 |
: fc_(fc), |
| 1404 |
parserAtoms_(parserAtoms), |
| 1405 |
moduleFunctionNode_(moduleFunctionNode), |
| 1406 |
moduleFunctionName_(FunctionName(moduleFunctionNode)), |
| 1407 |
standardLibraryMathNames_(fc), |
| 1408 |
validationLifo_(VALIDATION_LIFO_DEFAULT_CHUNK_SIZE, js::MallocArena), |
| 1409 |
funcDefs_(fc), |
| 1410 |
tables_(fc), |
| 1411 |
globalMap_(fc), |
| 1412 |
sigSet_(fc), |
| 1413 |
funcImportMap_(fc), |
| 1414 |
arrayViews_(fc), |
| 1415 |
compilerEnv_(CompileMode::Once, Tier::Optimized, DebugEnabled::False), |
| 1416 |
moduleMeta_(moduleMeta), |
| 1417 |
codeMeta_(codeMeta) { |
| 1418 |
compilerEnv_.computeParameters(); |
| 1419 |
memory_.minLength = RoundUpToNextValidAsmJSHeapLength(0); |
| 1420 |
} |
| 1421 |
|
| 1422 |
protected: |
| 1423 |
[[nodiscard]] bool addStandardLibraryMathInfo() { |
| 1424 |
static constexpr struct { |
| 1425 |
const char* name; |
| 1426 |
AsmJSMathBuiltinFunction func; |
| 1427 |
} functions[] = { |
| 1428 |
{"sin", AsmJSMathBuiltin_sin}, {"cos", AsmJSMathBuiltin_cos}, |
| 1429 |
{"tan", AsmJSMathBuiltin_tan}, {"asin", AsmJSMathBuiltin_asin}, |
| 1430 |
{"acos", AsmJSMathBuiltin_acos}, {"atan", AsmJSMathBuiltin_atan}, |
| 1431 |
{"ceil", AsmJSMathBuiltin_ceil}, {"floor", AsmJSMathBuiltin_floor}, |
| 1432 |
{"exp", AsmJSMathBuiltin_exp}, {"log", AsmJSMathBuiltin_log}, |
| 1433 |
{"pow", AsmJSMathBuiltin_pow}, {"sqrt", AsmJSMathBuiltin_sqrt}, |
| 1434 |
{"abs", AsmJSMathBuiltin_abs}, {"atan2", AsmJSMathBuiltin_atan2}, |
| 1435 |
{"imul", AsmJSMathBuiltin_imul}, {"clz32", AsmJSMathBuiltin_clz32}, |
| 1436 |
{"fround", AsmJSMathBuiltin_fround}, {"min", AsmJSMathBuiltin_min}, |
| 1437 |
{"max", AsmJSMathBuiltin_max}, |
| 1438 |
}; |
| 1439 |
|
| 1440 |
auto AddMathFunction = [this](const char* name, |
| 1441 |
AsmJSMathBuiltinFunction func) { |
| 1442 |
auto atom = parserAtoms_.internAscii(fc_, name, strlen(name)); |
| 1443 |
if (!atom) { |
| 1444 |
return false; |
| 1445 |
} |
| 1446 |
MathBuiltin builtin(func); |
| 1447 |
return this->standardLibraryMathNames_.putNew(atom, builtin); |
| 1448 |
}; |
| 1449 |
|
| 1450 |
for (const auto& info : functions) { |
| 1451 |
if (!AddMathFunction(info.name, info.func)) { |
| 1452 |
return false; |
| 1453 |
} |
| 1454 |
} |
| 1455 |
|
| 1456 |
static constexpr struct { |
| 1457 |
const char* name; |
| 1458 |
double value; |
| 1459 |
} constants[] = { |
| 1460 |
{"E", M_E}, |
| 1461 |
{"LN10", M_LN10}, |
| 1462 |
{"LN2", M_LN2}, |
| 1463 |
{"LOG2E", M_LOG2E}, |
| 1464 |
{"LOG10E", M_LOG10E}, |
| 1465 |
{"PI", M_PI}, |
| 1466 |
{"SQRT1_2", M_SQRT1_2}, |
| 1467 |
{"SQRT2", M_SQRT2}, |
| 1468 |
}; |
| 1469 |
|
| 1470 |
auto AddMathConstant = [this](const char* name, double cst) { |
| 1471 |
auto atom = parserAtoms_.internAscii(fc_, name, strlen(name)); |
| 1472 |
if (!atom) { |
| 1473 |
return false; |
| 1474 |
} |
| 1475 |
MathBuiltin builtin(cst); |
| 1476 |
return this->standardLibraryMathNames_.putNew(atom, builtin); |
| 1477 |
}; |
| 1478 |
|
| 1479 |
for (const auto& info : constants) { |
| 1480 |
if (!AddMathConstant(info.name, info.value)) { |
| 1481 |
return false; |
| 1482 |
} |
| 1483 |
} |
| 1484 |
|
| 1485 |
return true; |
| 1486 |
} |
| 1487 |
|
| 1488 |
public: |
| 1489 |
FrontendContext* fc() const { return fc_; } |
| 1490 |
TaggedParserAtomIndex moduleFunctionName() const { |
| 1491 |
return moduleFunctionName_; |
| 1492 |
} |
| 1493 |
TaggedParserAtomIndex globalArgumentName() const { |
| 1494 |
return globalArgumentName_; |
| 1495 |
} |
| 1496 |
TaggedParserAtomIndex importArgumentName() const { |
| 1497 |
return importArgumentName_; |
| 1498 |
} |
| 1499 |
TaggedParserAtomIndex bufferArgumentName() const { |
| 1500 |
return bufferArgumentName_; |
| 1501 |
} |
| 1502 |
const CodeMetadata* codeMeta() { return codeMeta_; } |
| 1503 |
const ModuleMetadata* moduleMeta() { return moduleMeta_; } |
| 1504 |
|
| 1505 |
void initModuleFunctionName(TaggedParserAtomIndex name) { |
| 1506 |
MOZ_ASSERT(!moduleFunctionName_); |
| 1507 |
moduleFunctionName_ = name; |
| 1508 |
} |
| 1509 |
[[nodiscard]] bool initGlobalArgumentName(TaggedParserAtomIndex n) { |
| 1510 |
globalArgumentName_ = n; |
| 1511 |
if (n) { |
| 1512 |
codeMetaForAsmJS_->globalArgumentName = |
| 1513 |
parserAtoms_.toNewUTF8CharsZ(fc_, n); |
| 1514 |
if (!codeMetaForAsmJS_->globalArgumentName) { |
| 1515 |
return false; |
| 1516 |
} |
| 1517 |
} |
| 1518 |
return true; |
| 1519 |
} |
| 1520 |
[[nodiscard]] bool initImportArgumentName(TaggedParserAtomIndex n) { |
| 1521 |
importArgumentName_ = n; |
| 1522 |
if (n) { |
| 1523 |
codeMetaForAsmJS_->importArgumentName = |
| 1524 |
parserAtoms_.toNewUTF8CharsZ(fc_, n); |
| 1525 |
if (!codeMetaForAsmJS_->importArgumentName) { |
| 1526 |
return false; |
| 1527 |
} |
| 1528 |
} |
| 1529 |
return true; |
| 1530 |
} |
| 1531 |
[[nodiscard]] bool initBufferArgumentName(TaggedParserAtomIndex n) { |
| 1532 |
bufferArgumentName_ = n; |
| 1533 |
if (n) { |
| 1534 |
codeMetaForAsmJS_->bufferArgumentName = |
| 1535 |
parserAtoms_.toNewUTF8CharsZ(fc_, n); |
| 1536 |
if (!codeMetaForAsmJS_->bufferArgumentName) { |
| 1537 |
return false; |
| 1538 |
} |
| 1539 |
} |
| 1540 |
return true; |
| 1541 |
} |
| 1542 |
bool addGlobalVarInit(TaggedParserAtomIndex var, const NumLit& lit, Type type, |
| 1543 |
bool isConst) { |
| 1544 |
MOZ_ASSERT(type.isGlobalVarType()); |
| 1545 |
MOZ_ASSERT(type == Type::canonicalize(Type::lit(lit))); |
| 1546 |
|
| 1547 |
uint32_t index = codeMeta_->globals.length(); |
| 1548 |
if (!codeMeta_->globals.emplaceBack(type.canonicalToValType(), !isConst, |
| 1549 |
index, ModuleKind::AsmJS)) { |
| 1550 |
return false; |
| 1551 |
} |
| 1552 |
|
| 1553 |
Global::Which which = isConst ? Global::ConstantLiteral : Global::Variable; |
| 1554 |
Global* global = validationLifo_.new_<Global>(which); |
| 1555 |
if (!global) { |
| 1556 |
return false; |
| 1557 |
} |
| 1558 |
if (isConst) { |
| 1559 |
new (&global->u.varOrConst) Global::U::VarOrConst(index, lit); |
| 1560 |
} else { |
| 1561 |
new (&global->u.varOrConst) Global::U::VarOrConst(index, type.which()); |
| 1562 |
} |
| 1563 |
if (!globalMap_.putNew(var, global)) { |
| 1564 |
return false; |
| 1565 |
} |
| 1566 |
|
| 1567 |
AsmJSGlobal g(AsmJSGlobal::Variable, nullptr); |
| 1568 |
g.pod.u.var.initKind_ = AsmJSGlobal::InitConstant; |
| 1569 |
g.pod.u.var.u.val_ = lit.value(); |
| 1570 |
return codeMetaForAsmJS_->asmJSGlobals.append(std::move(g)); |
| 1571 |
} |
| 1572 |
bool addGlobalVarImport(TaggedParserAtomIndex var, |
| 1573 |
TaggedParserAtomIndex field, Type type, |
| 1574 |
bool isConst) { |
| 1575 |
MOZ_ASSERT(type.isGlobalVarType()); |
| 1576 |
|
| 1577 |
UniqueChars fieldChars = parserAtoms_.toNewUTF8CharsZ(fc_, field); |
| 1578 |
if (!fieldChars) { |
| 1579 |
return false; |
| 1580 |
} |
| 1581 |
|
| 1582 |
uint32_t index = codeMeta_->globals.length(); |
| 1583 |
ValType valType = type.canonicalToValType(); |
| 1584 |
if (!codeMeta_->globals.emplaceBack(valType, !isConst, index, |
| 1585 |
ModuleKind::AsmJS)) { |
| 1586 |
return false; |
| 1587 |
} |
| 1588 |
|
| 1589 |
Global::Which which = isConst ? Global::ConstantImport : Global::Variable; |
| 1590 |
Global* global = validationLifo_.new_<Global>(which); |
| 1591 |
if (!global) { |
| 1592 |
return false; |
| 1593 |
} |
| 1594 |
new (&global->u.varOrConst) Global::U::VarOrConst(index, type.which()); |
| 1595 |
if (!globalMap_.putNew(var, global)) { |
| 1596 |
return false; |
| 1597 |
} |
| 1598 |
|
| 1599 |
AsmJSGlobal g(AsmJSGlobal::Variable, std::move(fieldChars)); |
| 1600 |
g.pod.u.var.initKind_ = AsmJSGlobal::InitImport; |
| 1601 |
g.pod.u.var.u.importValType_ = valType.packed(); |
| 1602 |
return codeMetaForAsmJS_->asmJSGlobals.append(std::move(g)); |
| 1603 |
} |
| 1604 |
bool addArrayView(TaggedParserAtomIndex var, Scalar::Type vt, |
| 1605 |
TaggedParserAtomIndex maybeField) { |
| 1606 |
UniqueChars fieldChars; |
| 1607 |
if (maybeField) { |
| 1608 |
fieldChars = parserAtoms_.toNewUTF8CharsZ(fc_, maybeField); |
| 1609 |
if (!fieldChars) { |
| 1610 |
return false; |
| 1611 |
} |
| 1612 |
} |
| 1613 |
|
| 1614 |
if (!arrayViews_.append(ArrayView(var, vt))) { |
| 1615 |
return false; |
| 1616 |
} |
| 1617 |
|
| 1618 |
Global* global = validationLifo_.new_<Global>(Global::ArrayView); |
| 1619 |
if (!global) { |
| 1620 |
return false; |
| 1621 |
} |
| 1622 |
new (&global->u.viewType_) Scalar::Type(vt); |
| 1623 |
if (!globalMap_.putNew(var, global)) { |
| 1624 |
return false; |
| 1625 |
} |
| 1626 |
|
| 1627 |
AsmJSGlobal g(AsmJSGlobal::ArrayView, std::move(fieldChars)); |
| 1628 |
g.pod.u.viewType_ = vt; |
| 1629 |
return codeMetaForAsmJS_->asmJSGlobals.append(std::move(g)); |
| 1630 |
} |
| 1631 |
bool addMathBuiltinFunction(TaggedParserAtomIndex var, |
| 1632 |
AsmJSMathBuiltinFunction func, |
| 1633 |
TaggedParserAtomIndex field) { |
| 1634 |
UniqueChars fieldChars = parserAtoms_.toNewUTF8CharsZ(fc_, field); |
| 1635 |
if (!fieldChars) { |
| 1636 |
return false; |
| 1637 |
} |
| 1638 |
|
| 1639 |
Global* global = validationLifo_.new_<Global>(Global::MathBuiltinFunction); |
| 1640 |
if (!global) { |
| 1641 |
return false; |
| 1642 |
} |
| 1643 |
new (&global->u.mathBuiltinFunc_) AsmJSMathBuiltinFunction(func); |
| 1644 |
if (!globalMap_.putNew(var, global)) { |
| 1645 |
return false; |
| 1646 |
} |
| 1647 |
|
| 1648 |
AsmJSGlobal g(AsmJSGlobal::MathBuiltinFunction, std::move(fieldChars)); |
| 1649 |
g.pod.u.mathBuiltinFunc_ = func; |
| 1650 |
return codeMetaForAsmJS_->asmJSGlobals.append(std::move(g)); |
| 1651 |
} |
| 1652 |
|
| 1653 |
private: |
| 1654 |
bool addGlobalDoubleConstant(TaggedParserAtomIndex var, double constant) { |
| 1655 |
Global* global = validationLifo_.new_<Global>(Global::ConstantLiteral); |
| 1656 |
if (!global) { |
| 1657 |
return false; |
| 1658 |
} |
| 1659 |
new (&global->u.varOrConst) Global::U::VarOrConst(constant); |
| 1660 |
return globalMap_.putNew(var, global); |
| 1661 |
} |
| 1662 |
|
| 1663 |
public: |
| 1664 |
bool addMathBuiltinConstant(TaggedParserAtomIndex var, double constant, |
| 1665 |
TaggedParserAtomIndex field) { |
| 1666 |
UniqueChars fieldChars = parserAtoms_.toNewUTF8CharsZ(fc_, field); |
| 1667 |
if (!fieldChars) { |
| 1668 |
return false; |
| 1669 |
} |
| 1670 |
|
| 1671 |
if (!addGlobalDoubleConstant(var, constant)) { |
| 1672 |
return false; |
| 1673 |
} |
| 1674 |
|
| 1675 |
AsmJSGlobal g(AsmJSGlobal::Constant, std::move(fieldChars)); |
| 1676 |
g.pod.u.constant.value_ = constant; |
| 1677 |
g.pod.u.constant.kind_ = AsmJSGlobal::MathConstant; |
| 1678 |
return codeMetaForAsmJS_->asmJSGlobals.append(std::move(g)); |
| 1679 |
} |
| 1680 |
bool addGlobalConstant(TaggedParserAtomIndex var, double constant, |
| 1681 |
TaggedParserAtomIndex field) { |
| 1682 |
UniqueChars fieldChars = parserAtoms_.toNewUTF8CharsZ(fc_, field); |
| 1683 |
if (!fieldChars) { |
| 1684 |
return false; |
| 1685 |
} |
| 1686 |
|
| 1687 |
if (!addGlobalDoubleConstant(var, constant)) { |
| 1688 |
return false; |
| 1689 |
} |
| 1690 |
|
| 1691 |
AsmJSGlobal g(AsmJSGlobal::Constant, std::move(fieldChars)); |
| 1692 |
g.pod.u.constant.value_ = constant; |
| 1693 |
g.pod.u.constant.kind_ = AsmJSGlobal::GlobalConstant; |
| 1694 |
return codeMetaForAsmJS_->asmJSGlobals.append(std::move(g)); |
| 1695 |
} |
| 1696 |
bool addArrayViewCtor(TaggedParserAtomIndex var, Scalar::Type vt, |
| 1697 |
TaggedParserAtomIndex field) { |
| 1698 |
UniqueChars fieldChars = parserAtoms_.toNewUTF8CharsZ(fc_, field); |
| 1699 |
if (!fieldChars) { |
| 1700 |
return false; |
| 1701 |
} |
| 1702 |
|
| 1703 |
Global* global = validationLifo_.new_<Global>(Global::ArrayViewCtor); |
| 1704 |
if (!global) { |
| 1705 |
return false; |
| 1706 |
} |
| 1707 |
new (&global->u.viewType_) Scalar::Type(vt); |
| 1708 |
if (!globalMap_.putNew(var, global)) { |
| 1709 |
return false; |
| 1710 |
} |
| 1711 |
|
| 1712 |
AsmJSGlobal g(AsmJSGlobal::ArrayViewCtor, std::move(fieldChars)); |
| 1713 |
g.pod.u.viewType_ = vt; |
| 1714 |
return codeMetaForAsmJS_->asmJSGlobals.append(std::move(g)); |
| 1715 |
} |
| 1716 |
bool addFFI(TaggedParserAtomIndex var, TaggedParserAtomIndex field) { |
| 1717 |
UniqueChars fieldChars = parserAtoms_.toNewUTF8CharsZ(fc_, field); |
| 1718 |
if (!fieldChars) { |
| 1719 |
return false; |
| 1720 |
} |
| 1721 |
|
| 1722 |
if (codeMetaForAsmJS_->numFFIs == UINT32_MAX) { |
| 1723 |
return false; |
| 1724 |
} |
| 1725 |
uint32_t ffiIndex = codeMetaForAsmJS_->numFFIs++; |
| 1726 |
|
| 1727 |
Global* global = validationLifo_.new_<Global>(Global::FFI); |
| 1728 |
if (!global) { |
| 1729 |
return false; |
| 1730 |
} |
| 1731 |
new (&global->u.ffiIndex_) uint32_t(ffiIndex); |
| 1732 |
if (!globalMap_.putNew(var, global)) { |
| 1733 |
return false; |
| 1734 |
} |
| 1735 |
|
| 1736 |
AsmJSGlobal g(AsmJSGlobal::FFI, std::move(fieldChars)); |
| 1737 |
g.pod.u.ffiIndex_ = ffiIndex; |
| 1738 |
return codeMetaForAsmJS_->asmJSGlobals.append(std::move(g)); |
| 1739 |
} |
| 1740 |
bool addExportField(const Func& func, TaggedParserAtomIndex maybeField) { |
| 1741 |
// Record the field name of this export. |
| 1742 |
CacheableName fieldName; |
| 1743 |
if (maybeField) { |
| 1744 |
UniqueChars fieldChars = parserAtoms_.toNewUTF8CharsZ(fc_, maybeField); |
| 1745 |
if (!fieldChars) { |
| 1746 |
return false; |
| 1747 |
} |
| 1748 |
fieldName = CacheableName::fromUTF8Chars(std::move(fieldChars)); |
| 1749 |
} |
| 1750 |
|
| 1751 |
// Declare which function is exported which gives us an index into the |
| 1752 |
// module ExportVector. |
| 1753 |
uint32_t funcIndex = funcImportMap_.count() + func.funcDefIndex(); |
| 1754 |
if (!moduleMeta_->exports.emplaceBack(std::move(fieldName), funcIndex, |
| 1755 |
DefinitionKind::Function)) { |
| 1756 |
return false; |
| 1757 |
} |
| 1758 |
|
| 1759 |
// The exported function might have already been exported in which case |
| 1760 |
// the index will refer into the range of AsmJSExports. |
| 1761 |
return codeMetaForAsmJS_->asmJSExports.emplaceBack( |
| 1762 |
funcIndex, func.srcBegin() - codeMetaForAsmJS_->srcStart, |
| 1763 |
func.srcEnd() - codeMetaForAsmJS_->srcStart); |
| 1764 |
} |
| 1765 |
|
| 1766 |
bool defineFuncPtrTable(uint32_t tableIndex, Uint32Vector&& elems) { |
| 1767 |
Table& table = *tables_[tableIndex]; |
| 1768 |
if (table.defined()) { |
| 1769 |
return false; |
| 1770 |
} |
| 1771 |
|
| 1772 |
table.define(); |
| 1773 |
|
| 1774 |
for (uint32_t& index : elems) { |
| 1775 |
index += funcImportMap_.count(); |
| 1776 |
} |
| 1777 |
|
| 1778 |
ModuleElemSegment seg = ModuleElemSegment(); |
| 1779 |
seg.elemType = RefType::func(); |
| 1780 |
seg.tableIndex = tableIndex; |
| 1781 |
seg.offsetIfActive = Some(InitExpr(LitVal(uint32_t(0)))); |
| 1782 |
seg.encoding = ModuleElemSegment::Encoding::Indices; |
| 1783 |
seg.elemIndices = std::move(elems); |
| 1784 |
bool ok = codeMeta_->elemSegmentTypes.append(seg.elemType) && |
| 1785 |
moduleMeta_->elemSegments.append(std::move(seg)); |
| 1786 |
MOZ_ASSERT_IF(ok, codeMeta_->elemSegmentTypes.length() == |
| 1787 |
moduleMeta_->elemSegments.length()); |
| 1788 |
return ok; |
| 1789 |
} |
| 1790 |
|
| 1791 |
bool tryConstantAccess(uint64_t start, uint64_t width) { |
| 1792 |
MOZ_ASSERT(UINT64_MAX - start > width); |
| 1793 |
uint64_t len = start + width; |
| 1794 |
if (len > uint64_t(INT32_MAX) + 1) { |
| 1795 |
return false; |
| 1796 |
} |
| 1797 |
len = RoundUpToNextValidAsmJSHeapLength(len); |
| 1798 |
if (len > memory_.minLength) { |
| 1799 |
memory_.minLength = len; |
| 1800 |
} |
| 1801 |
return true; |
| 1802 |
} |
| 1803 |
|
| 1804 |
// Error handling. |
| 1805 |
bool hasAlreadyFailed() const { return !!errorString_; } |
| 1806 |
|
| 1807 |
bool failOffset(uint32_t offset, const char* str) { |
| 1808 |
MOZ_ASSERT(!hasAlreadyFailed()); |
| 1809 |
MOZ_ASSERT(errorOffset_ == UINT32_MAX); |
| 1810 |
MOZ_ASSERT(str); |
| 1811 |
errorOffset_ = offset; |
| 1812 |
errorString_ = DuplicateString(str); |
| 1813 |
return false; |
| 1814 |
} |
| 1815 |
|
| 1816 |
bool fail(ParseNode* pn, const char* str) { |
| 1817 |
return failOffset(pn->pn_pos.begin, str); |
| 1818 |
} |
| 1819 |
|
| 1820 |
bool failfVAOffset(uint32_t offset, const char* fmt, va_list ap) |
| 1821 |
MOZ_FORMAT_PRINTF(3, 0) { |
| 1822 |
MOZ_ASSERT(!hasAlreadyFailed()); |
| 1823 |
MOZ_ASSERT(errorOffset_ == UINT32_MAX); |
| 1824 |
MOZ_ASSERT(fmt); |
| 1825 |
errorOffset_ = offset; |
| 1826 |
errorString_ = JS_vsmprintf(fmt, ap); |
| 1827 |
return false; |
| 1828 |
} |
| 1829 |
|
| 1830 |
bool failfOffset(uint32_t offset, const char* fmt, ...) |
| 1831 |
MOZ_FORMAT_PRINTF(3, 4) { |
| 1832 |
va_list ap; |
| 1833 |
va_start(ap, fmt); |
| 1834 |
failfVAOffset(offset, fmt, ap); |
| 1835 |
va_end(ap); |
| 1836 |
return false; |
| 1837 |
} |
| 1838 |
|
| 1839 |
bool failf(ParseNode* pn, const char* fmt, ...) MOZ_FORMAT_PRINTF(3, 4) { |
| 1840 |
va_list ap; |
| 1841 |
va_start(ap, fmt); |
| 1842 |
failfVAOffset(pn->pn_pos.begin, fmt, ap); |
| 1843 |
va_end(ap); |
| 1844 |
return false; |
| 1845 |
} |
| 1846 |
|
| 1847 |
bool failNameOffset(uint32_t offset, const char* fmt, |
| 1848 |
TaggedParserAtomIndex name) { |
| 1849 |
// This function is invoked without the caller properly rooting its locals. |
| 1850 |
if (UniqueChars bytes = parserAtoms_.toPrintableString(name)) { |
| 1851 |
failfOffset(offset, fmt, bytes.get()); |
| 1852 |
} else { |
| 1853 |
ReportOutOfMemory(fc_); |
| 1854 |
} |
| 1855 |
return false; |
| 1856 |
} |
| 1857 |
|
| 1858 |
bool failName(ParseNode* pn, const char* fmt, TaggedParserAtomIndex name) { |
| 1859 |
return failNameOffset(pn->pn_pos.begin, fmt, name); |
| 1860 |
} |
| 1861 |
|
| 1862 |
bool failOverRecursed() { |
| 1863 |
errorOverRecursed_ = true; |
| 1864 |
return false; |
| 1865 |
} |
| 1866 |
|
| 1867 |
unsigned numArrayViews() const { return arrayViews_.length(); } |
| 1868 |
const ArrayView& arrayView(unsigned i) const { return arrayViews_[i]; } |
| 1869 |
unsigned numFuncDefs() const { return funcDefs_.length(); } |
| 1870 |
const Func& funcDef(unsigned i) const { return funcDefs_[i]; } |
| 1871 |
unsigned numFuncPtrTables() const { return tables_.length(); } |
| 1872 |
Table& table(unsigned i) const { return *tables_[i]; } |
| 1873 |
|
| 1874 |
const Global* lookupGlobal(TaggedParserAtomIndex name) const { |
| 1875 |
if (GlobalMap::Ptr p = globalMap_.lookup(name)) { |
| 1876 |
return p->value(); |
| 1877 |
} |
| 1878 |
return nullptr; |
| 1879 |
} |
| 1880 |
|
| 1881 |
Func* lookupFuncDef(TaggedParserAtomIndex name) { |
| 1882 |
if (GlobalMap::Ptr p = globalMap_.lookup(name)) { |
| 1883 |
Global* value = p->value(); |
| 1884 |
if (value->which() == Global::Function) { |
| 1885 |
return &funcDefs_[value->funcDefIndex()]; |
| 1886 |
} |
| 1887 |
} |
| 1888 |
return nullptr; |
| 1889 |
} |
| 1890 |
|
| 1891 |
bool lookupStandardLibraryMathName(TaggedParserAtomIndex name, |
| 1892 |
MathBuiltin* mathBuiltin) const { |
| 1893 |
if (MathNameMap::Ptr p = standardLibraryMathNames_.lookup(name)) { |
| 1894 |
*mathBuiltin = p->value(); |
| 1895 |
return true; |
| 1896 |
} |
| 1897 |
return false; |
| 1898 |
} |
| 1899 |
|
| 1900 |
bool startFunctionBodies() { |
| 1901 |
if (!arrayViews_.empty()) { |
| 1902 |
memory_.usage = MemoryUsage::Unshared; |
| 1903 |
} else { |
| 1904 |
memory_.usage = MemoryUsage::None; |
| 1905 |
} |
| 1906 |
return true; |
| 1907 |
} |
| 1908 |
}; |
| 1909 |
|
| 1910 |
// The ModuleValidator encapsulates the entire validation of an asm.js module. |
| 1911 |
// Its lifetime goes from the validation of the top components of an asm.js |
| 1912 |
// module (all the globals), the emission of bytecode for all the functions in |
| 1913 |
// the module and the validation of function's pointer tables. It also finishes |
| 1914 |
// the compilation of all the module's stubs. |
| 1915 |
template <typename Unit> |
| 1916 |
class MOZ_STACK_CLASS ModuleValidator : public ModuleValidatorShared { |
| 1917 |
private: |
| 1918 |
AsmJSParser<Unit>& parser_; |
| 1919 |
|
| 1920 |
public: |
| 1921 |
ModuleValidator(FrontendContext* fc, ParserAtomsTable& parserAtoms, |
| 1922 |
MutableModuleMetadata moduleMeta, |
| 1923 |
MutableCodeMetadata codeMeta, AsmJSParser<Unit>& parser, |
| 1924 |
FunctionNode* moduleFunctionNode) |
| 1925 |
: ModuleValidatorShared(fc, parserAtoms, moduleMeta, codeMeta, |
| 1926 |
moduleFunctionNode), |
| 1927 |
parser_(parser) {} |
| 1928 |
|
| 1929 |
~ModuleValidator() { |
| 1930 |
if (errorString_) { |
| 1931 |
MOZ_ASSERT(errorOffset_ != UINT32_MAX); |
| 1932 |
typeFailure(errorOffset_, errorString_.get()); |
| 1933 |
} |
| 1934 |
if (errorOverRecursed_) { |
| 1935 |
ReportOverRecursed(fc_); |
| 1936 |
} |
| 1937 |
} |
| 1938 |
|
| 1939 |
private: |
| 1940 |
// Helpers: |
| 1941 |
bool newSig(FuncType&& sig, uint32_t* sigIndex) { |
| 1942 |
if (codeMeta_->types->length() >= MaxTypes) { |
| 1943 |
return failCurrentOffset("too many signatures"); |
| 1944 |
} |
| 1945 |
|
| 1946 |
*sigIndex = codeMeta_->types->length(); |
| 1947 |
return codeMeta_->types->addType(std::move(sig)); |
| 1948 |
} |
| 1949 |
bool declareSig(FuncType&& sig, uint32_t* sigIndex) { |
| 1950 |
SigSet::AddPtr p = sigSet_.lookupForAdd(sig); |
| 1951 |
if (p) { |
| 1952 |
*sigIndex = p->sigIndex(); |
| 1953 |
MOZ_ASSERT(FuncType::strictlyEquals( |
| 1954 |
codeMeta_->types->type(*sigIndex).funcType(), sig)); |
| 1955 |
return true; |
| 1956 |
} |
| 1957 |
|
| 1958 |
return newSig(std::move(sig), sigIndex) && |
| 1959 |
sigSet_.add(p, HashableSig(*sigIndex, *codeMeta_->types)); |
| 1960 |
} |
| 1961 |
|
| 1962 |
private: |
| 1963 |
void typeFailure(uint32_t offset, ...) { |
| 1964 |
va_list args; |
| 1965 |
va_start(args, offset); |
| 1966 |
|
| 1967 |
auto& ts = tokenStream(); |
| 1968 |
ErrorMetadata metadata; |
| 1969 |
if (ts.computeErrorMetadata(&metadata, AsVariant(offset))) { |
| 1970 |
if (ts.anyCharsAccess().options().throwOnAsmJSValidationFailure()) { |
| 1971 |
ReportCompileErrorLatin1VA(fc_, std::move(metadata), nullptr, |
| 1972 |
JSMSG_USE_ASM_TYPE_FAIL, &args); |
| 1973 |
} else { |
| 1974 |
// asm.js type failure is indicated by calling one of the fail* |
| 1975 |
// functions below. These functions always return false to |
| 1976 |
// halt asm.js parsing. Whether normal parsing is attempted as |
| 1977 |
// fallback, depends whether an exception is also set. |
| 1978 |
// |
| 1979 |
// If warning succeeds, no exception is set. If warning fails, |
| 1980 |
// an exception is set and execution will halt. Thus it's safe |
| 1981 |
// and correct to ignore the return value here. |
| 1982 |
(void)ts.compileWarning(std::move(metadata), nullptr, |
| 1983 |
JSMSG_USE_ASM_TYPE_FAIL, &args); |
| 1984 |
} |
| 1985 |
} |
| 1986 |
|
| 1987 |
va_end(args); |
| 1988 |
} |
| 1989 |
|
| 1990 |
public: |
| 1991 |
bool init() { |
| 1992 |
codeMetaForAsmJS_ = js_new<CodeMetadataForAsmJSImpl>(); |
| 1993 |
if (!codeMetaForAsmJS_) { |
| 1994 |
ReportOutOfMemory(fc_); |
| 1995 |
return false; |
| 1996 |
} |
| 1997 |
|
| 1998 |
codeMetaForAsmJS_->toStringStart = |
| 1999 |
moduleFunctionNode_->funbox()->extent().toStringStart; |
| 2000 |
codeMetaForAsmJS_->srcStart = moduleFunctionNode_->body()->pn_pos.begin; |
| 2001 |
codeMetaForAsmJS_->strict = parser_.pc_->sc()->strict() && |
| 2002 |
!parser_.pc_->sc()->hasExplicitUseStrict(); |
| 2003 |
codeMetaForAsmJS_->alwaysUseFdlibm = parser_.options().alwaysUseFdlibm(); |
| 2004 |
codeMetaForAsmJS_->source = do_AddRef(parser_.ss); |
| 2005 |
|
| 2006 |
return addStandardLibraryMathInfo(); |
| 2007 |
} |
| 2008 |
|
| 2009 |
AsmJSParser<Unit>& parser() const { return parser_; } |
| 2010 |
|
| 2011 |
auto& tokenStream() const { return parser_.tokenStream; } |
| 2012 |
|
| 2013 |
bool alwaysUseFdlibm() const { return codeMetaForAsmJS_->alwaysUseFdlibm; } |
| 2014 |
|
| 2015 |
public: |
| 2016 |
bool addFuncDef(TaggedParserAtomIndex name, uint32_t firstUse, FuncType&& sig, |
| 2017 |
Func** func) { |
| 2018 |
uint32_t sigIndex; |
| 2019 |
if (!declareSig(std::move(sig), &sigIndex)) { |
| 2020 |
return false; |
| 2021 |
} |
| 2022 |
|
| 2023 |
uint32_t funcDefIndex = funcDefs_.length(); |
| 2024 |
if (funcDefIndex >= MaxFuncs) { |
| 2025 |
return failCurrentOffset("too many functions"); |
| 2026 |
} |
| 2027 |
|
| 2028 |
Global* global = validationLifo_.new_<Global>(Global::Function); |
| 2029 |
if (!global) { |
| 2030 |
return false; |
| 2031 |
} |
| 2032 |
new (&global->u.funcDefIndex_) uint32_t(funcDefIndex); |
| 2033 |
if (!globalMap_.putNew(name, global)) { |
| 2034 |
return false; |
| 2035 |
} |
| 2036 |
if (!funcDefs_.emplaceBack(name, sigIndex, firstUse, funcDefIndex)) { |
| 2037 |
return false; |
| 2038 |
} |
| 2039 |
*func = &funcDefs_.back(); |
| 2040 |
return true; |
| 2041 |
} |
| 2042 |
bool declareFuncPtrTable(FuncType&& sig, TaggedParserAtomIndex name, |
| 2043 |
uint32_t firstUse, uint32_t mask, |
| 2044 |
uint32_t* tableIndex) { |
| 2045 |
if (mask > MaxTableElemsRuntime) { |
| 2046 |
return failCurrentOffset("function pointer table too big"); |
| 2047 |
} |
| 2048 |
|
| 2049 |
MOZ_ASSERT(codeMeta_->tables.length() == tables_.length()); |
| 2050 |
*tableIndex = codeMeta_->tables.length(); |
| 2051 |
|
| 2052 |
uint32_t sigIndex; |
| 2053 |
if (!newSig(std::move(sig), &sigIndex)) { |
| 2054 |
return false; |
| 2055 |
} |
| 2056 |
|
| 2057 |
MOZ_ASSERT(sigIndex >= codeMeta_->asmJSSigToTableIndex.length()); |
| 2058 |
if (!codeMeta_->asmJSSigToTableIndex.resize(sigIndex + 1)) { |
| 2059 |
return false; |
| 2060 |
} |
| 2061 |
|
| 2062 |
Limits limits = Limits(mask + 1, Nothing(), Shareable::False); |
| 2063 |
codeMeta_->asmJSSigToTableIndex[sigIndex] = codeMeta_->tables.length(); |
| 2064 |
if (!codeMeta_->tables.emplaceBack(limits, RefType::func(), |
| 2065 |
/* initExpr */ Nothing(), |
| 2066 |
/*isAsmJS*/ true)) { |
| 2067 |
return false; |
| 2068 |
} |
| 2069 |
|
| 2070 |
Global* global = validationLifo_.new_<Global>(Global::Table); |
| 2071 |
if (!global) { |
| 2072 |
return false; |
| 2073 |
} |
| 2074 |
|
| 2075 |
new (&global->u.tableIndex_) uint32_t(*tableIndex); |
| 2076 |
if (!globalMap_.putNew(name, global)) { |
| 2077 |
return false; |
| 2078 |
} |
| 2079 |
|
| 2080 |
Table* t = validationLifo_.new_<Table>(sigIndex, name, firstUse, mask); |
| 2081 |
return t && tables_.append(t); |
| 2082 |
} |
| 2083 |
bool declareImport(TaggedParserAtomIndex name, FuncType&& sig, |
| 2084 |
unsigned ffiIndex, uint32_t* importIndex) { |
| 2085 |
FuncImportMap::AddPtr p = |
| 2086 |
funcImportMap_.lookupForAdd(NamedSig::Lookup(name, sig)); |
| 2087 |
if (p) { |
| 2088 |
*importIndex = p->value(); |
| 2089 |
return true; |
| 2090 |
} |
| 2091 |
|
| 2092 |
*importIndex = funcImportMap_.count(); |
| 2093 |
MOZ_ASSERT(*importIndex == codeMetaForAsmJS_->asmJSImports.length()); |
| 2094 |
|
| 2095 |
if (*importIndex >= MaxImports) { |
| 2096 |
return failCurrentOffset("too many imports"); |
| 2097 |
} |
| 2098 |
|
| 2099 |
if (!codeMetaForAsmJS_->asmJSImports.emplaceBack(ffiIndex)) { |
| 2100 |
return false; |
| 2101 |
} |
| 2102 |
|
| 2103 |
uint32_t sigIndex; |
| 2104 |
if (!declareSig(std::move(sig), &sigIndex)) { |
| 2105 |
return false; |
| 2106 |
} |
| 2107 |
|
| 2108 |
return funcImportMap_.add(p, NamedSig(name, sigIndex, *codeMeta_->types), |
| 2109 |
*importIndex); |
| 2110 |
} |
| 2111 |
|
| 2112 |
// Error handling. |
| 2113 |
bool failCurrentOffset(const char* str) { |
| 2114 |
return failOffset(tokenStream().anyCharsAccess().currentToken().pos.begin, |
| 2115 |
str); |
| 2116 |
} |
| 2117 |
|
| 2118 |
SharedModule finish() { |
| 2119 |
MOZ_ASSERT(codeMeta_->numMemories() == 0); |
| 2120 |
if (memory_.usage != MemoryUsage::None) { |
| 2121 |
Limits limits; |
| 2122 |
limits.shared = memory_.usage == MemoryUsage::Shared ? Shareable::True |
| 2123 |
: Shareable::False; |
| 2124 |
limits.initial = memory_.minPages(); |
| 2125 |
limits.maximum = Nothing(); |
| 2126 |
limits.addressType = AddressType::I32; |
| 2127 |
if (!codeMeta_->memories.append(MemoryDesc(limits))) { |
| 2128 |
return nullptr; |
| 2129 |
} |
| 2130 |
} |
| 2131 |
MOZ_ASSERT(codeMeta_->funcs.empty()); |
| 2132 |
if (!codeMeta_->funcs.resize(funcImportMap_.count() + funcDefs_.length())) { |
| 2133 |
return nullptr; |
| 2134 |
} |
| 2135 |
for (FuncImportMap::Range r = funcImportMap_.all(); !r.empty(); |
| 2136 |
r.popFront()) { |
| 2137 |
uint32_t funcIndex = r.front().value(); |
| 2138 |
uint32_t funcTypeIndex = r.front().key().sigIndex(); |
| 2139 |
codeMeta_->funcs[funcIndex] = FuncDesc(funcTypeIndex); |
| 2140 |
} |
| 2141 |
for (const Func& func : funcDefs_) { |
| 2142 |
uint32_t funcIndex = funcImportMap_.count() + func.funcDefIndex(); |
| 2143 |
uint32_t funcTypeIndex = func.sigIndex(); |
| 2144 |
codeMeta_->funcs[funcIndex] = FuncDesc(funcTypeIndex); |
| 2145 |
} |
| 2146 |
for (const Export& exp : moduleMeta_->exports) { |
| 2147 |
if (exp.kind() != DefinitionKind::Function) { |
| 2148 |
continue; |
| 2149 |
} |
| 2150 |
uint32_t funcIndex = exp.funcIndex(); |
| 2151 |
codeMeta_->funcs[funcIndex].declareFuncExported(/* eager */ true, |
| 2152 |
/* canRefFunc */ false); |
| 2153 |
} |
| 2154 |
|
| 2155 |
codeMeta_->numFuncImports = funcImportMap_.count(); |
| 2156 |
|
| 2157 |
// All globals (inits and imports) are imports from Wasm point of view. |
| 2158 |
codeMeta_->numGlobalImports = codeMeta_->globals.length(); |
| 2159 |
|
| 2160 |
MOZ_ASSERT(codeMetaForAsmJS_->asmJSFuncNames.empty()); |
| 2161 |
if (!codeMetaForAsmJS_->asmJSFuncNames.resize(funcImportMap_.count())) { |
| 2162 |
return nullptr; |
| 2163 |
} |
| 2164 |
for (const Func& func : funcDefs_) { |
| 2165 |
CacheableChars funcName = parserAtoms_.toNewUTF8CharsZ(fc_, func.name()); |
| 2166 |
if (!funcName || |
| 2167 |
!codeMetaForAsmJS_->asmJSFuncNames.emplaceBack(std::move(funcName))) { |
| 2168 |
return nullptr; |
| 2169 |
} |
| 2170 |
} |
| 2171 |
|
| 2172 |
uint32_t endBeforeCurly = |
| 2173 |
tokenStream().anyCharsAccess().currentToken().pos.end; |
| 2174 |
codeMetaForAsmJS_->srcLength = endBeforeCurly - codeMetaForAsmJS_->srcStart; |
| 2175 |
|
| 2176 |
TokenPos pos; |
| 2177 |
MOZ_ALWAYS_TRUE( |
| 2178 |
tokenStream().peekTokenPos(&pos, TokenStreamShared::SlashIsRegExp)); |
| 2179 |
uint32_t endAfterCurly = pos.end; |
| 2180 |
codeMetaForAsmJS_->srcLengthWithRightBrace = |
| 2181 |
endAfterCurly - codeMetaForAsmJS_->srcStart; |
| 2182 |
|
| 2183 |
uint32_t codeSectionSize = 0; |
| 2184 |
for (const Func& func : funcDefs_) { |
| 2185 |
codeSectionSize += func.bytes().length(); |
| 2186 |
} |
| 2187 |
|
| 2188 |
codeMeta_->codeSectionRange = Some(BytecodeRange(0, codeSectionSize)); |
| 2189 |
|
| 2190 |
// asm.js does not have any wasm bytecode to save; view-source is |
| 2191 |
// provided through the ScriptSource. |
| 2192 |
BytecodeBufferOrSource bytecode; |
| 2193 |
|
| 2194 |
if (!moduleMeta_->prepareForCompile(compilerEnv_.mode())) { |
| 2195 |
return nullptr; |
| 2196 |
} |
| 2197 |
|
| 2198 |
ModuleGenerator mg(*codeMeta_, compilerEnv_, compilerEnv_.initialState(), |
| 2199 |
nullptr, nullptr, nullptr); |
| 2200 |
if (!mg.initializeCompleteTier(codeMetaForAsmJS_.get())) { |
| 2201 |
return nullptr; |
| 2202 |
} |
| 2203 |
|
| 2204 |
for (Func& func : funcDefs_) { |
| 2205 |
if (!mg.compileFuncDef(funcImportMap_.count() + func.funcDefIndex(), |
| 2206 |
func.line(), func.bytes().begin(), |
| 2207 |
func.bytes().end(), |
| 2208 |
std::move(func.callSiteLineNums()))) { |
| 2209 |
return nullptr; |
| 2210 |
} |
| 2211 |
} |
| 2212 |
|
| 2213 |
if (!mg.finishFuncDefs()) { |
| 2214 |
return nullptr; |
| 2215 |
} |
| 2216 |
|
| 2217 |
return mg.finishModule(bytecode, *moduleMeta_, |
| 2218 |
/*maybeCompleteTier2Listener=*/nullptr); |
| 2219 |
} |
| 2220 |
}; |
| 2221 |
|
| 2222 |
/*****************************************************************************/ |
| 2223 |
// Numeric literal utilities |
| 2224 |
|
| 2225 |
static bool IsNumericNonFloatLiteral(ParseNode* pn) { |
| 2226 |
// Note: '-' is never rolled into the number; numbers are always positive |
| 2227 |
// and negations must be applied manually. |
| 2228 |
return pn->isKind(ParseNodeKind::NumberExpr) || |
| 2229 |
(pn->isKind(ParseNodeKind::NegExpr) && |
| 2230 |
UnaryKid(pn)->isKind(ParseNodeKind::NumberExpr)); |
| 2231 |
} |
| 2232 |
|
| 2233 |
static bool IsCallToGlobal(ModuleValidatorShared& m, ParseNode* pn, |
| 2234 |
const ModuleValidatorShared::Global** global) { |
| 2235 |
if (!pn->isKind(ParseNodeKind::CallExpr)) { |
| 2236 |
return false; |
| 2237 |
} |
| 2238 |
|
| 2239 |
ParseNode* callee = CallCallee(pn); |
| 2240 |
if (!callee->isKind(ParseNodeKind::Name)) { |
| 2241 |
return false; |
| 2242 |
} |
| 2243 |
|
| 2244 |
*global = m.lookupGlobal(callee->as<NameNode>().name()); |
| 2245 |
return !!*global; |
| 2246 |
} |
| 2247 |
|
| 2248 |
static bool IsCoercionCall(ModuleValidatorShared& m, ParseNode* pn, |
| 2249 |
Type* coerceTo, ParseNode** coercedExpr) { |
| 2250 |
const ModuleValidatorShared::Global* global; |
| 2251 |
if (!IsCallToGlobal(m, pn, &global)) { |
| 2252 |
return false; |
| 2253 |
} |
| 2254 |
|
| 2255 |
if (CallArgListLength(pn) != 1) { |
| 2256 |
return false; |
| 2257 |
} |
| 2258 |
|
| 2259 |
if (coercedExpr) { |
| 2260 |
*coercedExpr = CallArgList(pn); |
| 2261 |
} |
| 2262 |
|
| 2263 |
if (global->isMathFunction() && |
| 2264 |
global->mathBuiltinFunction() == AsmJSMathBuiltin_fround) { |
| 2265 |
*coerceTo = Type::Float; |
| 2266 |
return true; |
| 2267 |
} |
| 2268 |
|
| 2269 |
return false; |
| 2270 |
} |
| 2271 |
|
| 2272 |
static bool IsFloatLiteral(ModuleValidatorShared& m, ParseNode* pn) { |
| 2273 |
ParseNode* coercedExpr; |
| 2274 |
Type coerceTo; |
| 2275 |
if (!IsCoercionCall(m, pn, &coerceTo, &coercedExpr)) { |
| 2276 |
return false; |
| 2277 |
} |
| 2278 |
// Don't fold into || to avoid clang/memcheck bug (bug 1077031). |
| 2279 |
if (!coerceTo.isFloat()) { |
| 2280 |
return false; |
| 2281 |
} |
| 2282 |
return IsNumericNonFloatLiteral(coercedExpr); |
| 2283 |
} |
| 2284 |
|
| 2285 |
static bool IsNumericLiteral(ModuleValidatorShared& m, ParseNode* pn) { |
| 2286 |
return IsNumericNonFloatLiteral(pn) || IsFloatLiteral(m, pn); |
| 2287 |
} |
| 2288 |
|
| 2289 |
// The JS grammar treats -42 as -(42) (i.e., with separate grammar |
| 2290 |
// productions) for the unary - and literal 42). However, the asm.js spec |
| 2291 |
// recognizes -42 (modulo parens, so -(42) and -((42))) as a single literal |
| 2292 |
// so fold the two potential parse nodes into a single double value. |
| 2293 |
static double ExtractNumericNonFloatValue(ParseNode* pn, |
| 2294 |
ParseNode** out = nullptr) { |
| 2295 |
MOZ_ASSERT(IsNumericNonFloatLiteral(pn)); |
| 2296 |
|
| 2297 |
if (pn->isKind(ParseNodeKind::NegExpr)) { |
| 2298 |
pn = UnaryKid(pn); |
| 2299 |
if (out) { |
| 2300 |
*out = pn; |
| 2301 |
} |
| 2302 |
return -NumberNodeValue(pn); |
| 2303 |
} |
| 2304 |
|
| 2305 |
return NumberNodeValue(pn); |
| 2306 |
} |
| 2307 |
|
| 2308 |
static NumLit ExtractNumericLiteral(ModuleValidatorShared& m, ParseNode* pn) { |
| 2309 |
MOZ_ASSERT(IsNumericLiteral(m, pn)); |
| 2310 |
|
| 2311 |
if (pn->isKind(ParseNodeKind::CallExpr)) { |
| 2312 |
// Float literals are explicitly coerced and thus the coerced literal may be |
| 2313 |
// any valid (non-float) numeric literal. |
| 2314 |
MOZ_ASSERT(CallArgListLength(pn) == 1); |
| 2315 |
pn = CallArgList(pn); |
| 2316 |
double d = ExtractNumericNonFloatValue(pn); |
| 2317 |
return NumLit(NumLit::Float, DoubleValue(d)); |
| 2318 |
} |
| 2319 |
|
| 2320 |
double d = ExtractNumericNonFloatValue(pn, &pn); |
| 2321 |
|
| 2322 |
// The asm.js spec syntactically distinguishes any literal containing a |
| 2323 |
// decimal point or the literal -0 as having double type. |
| 2324 |
if (NumberNodeHasFrac(pn) || IsNegativeZero(d)) { |
| 2325 |
return NumLit(NumLit::Double, DoubleValue(d)); |
| 2326 |
} |
| 2327 |
|
| 2328 |
// The syntactic checks above rule out these double values. |
| 2329 |
MOZ_ASSERT(!IsNegativeZero(d)); |
| 2330 |
MOZ_ASSERT(!std::isnan(d)); |
| 2331 |
|
| 2332 |
// Although doubles can only *precisely* represent 53-bit integers, they |
| 2333 |
// can *imprecisely* represent integers much bigger than an int64_t. |
| 2334 |
// Furthermore, d may be inf or -inf. In both cases, casting to an int64_t |
| 2335 |
// is undefined, so test against the integer bounds using doubles. |
| 2336 |
if (d < double(INT32_MIN) || d > double(UINT32_MAX)) { |
| 2337 |
return NumLit(NumLit::OutOfRangeInt, UndefinedValue()); |
| 2338 |
} |
| 2339 |
|
| 2340 |
// With the above syntactic and range limitations, d is definitely an |
| 2341 |
// integer in the range [INT32_MIN, UINT32_MAX] range. |
| 2342 |
int64_t i64 = int64_t(d); |
| 2343 |
if (i64 >= 0) { |
| 2344 |
if (i64 <= INT32_MAX) { |
| 2345 |
return NumLit(NumLit::Fixnum, Int32Value(i64)); |
| 2346 |
} |
| 2347 |
MOZ_ASSERT(i64 <= UINT32_MAX); |
| 2348 |
return NumLit(NumLit::BigUnsigned, Int32Value(uint32_t(i64))); |
| 2349 |
} |
| 2350 |
MOZ_ASSERT(i64 >= INT32_MIN); |
| 2351 |
return NumLit(NumLit::NegativeInt, Int32Value(i64)); |
| 2352 |
} |
| 2353 |
|
| 2354 |
static inline bool IsLiteralInt(const NumLit& lit, uint32_t* u32) { |
| 2355 |
switch (lit.which()) { |
| 2356 |
case NumLit::Fixnum: |
| 2357 |
case NumLit::BigUnsigned: |
| 2358 |
case NumLit::NegativeInt: |
| 2359 |
*u32 = lit.toUint32(); |
| 2360 |
return true; |
| 2361 |
case NumLit::Double: |
| 2362 |
case NumLit::Float: |
| 2363 |
case NumLit::OutOfRangeInt: |
| 2364 |
return false; |
| 2365 |
} |
| 2366 |
MOZ_CRASH("Bad literal type"); |
| 2367 |
} |
| 2368 |
|
| 2369 |
static inline bool IsLiteralInt(ModuleValidatorShared& m, ParseNode* pn, |
| 2370 |
uint32_t* u32) { |
| 2371 |
return IsNumericLiteral(m, pn) && |
| 2372 |
IsLiteralInt(ExtractNumericLiteral(m, pn), u32); |
| 2373 |
} |
| 2374 |
|
| 2375 |
/*****************************************************************************/ |
| 2376 |
|
| 2377 |
namespace { |
| 2378 |
|
| 2379 |
using LabelVector = Vector<TaggedParserAtomIndex, 4, SystemAllocPolicy>; |
| 2380 |
|
| 2381 |
class MOZ_STACK_CLASS FunctionValidatorShared { |
| 2382 |
public: |
| 2383 |
struct Local { |
| 2384 |
Type type; |
| 2385 |
unsigned slot; |
| 2386 |
Local(Type t, unsigned slot) : type(t), slot(slot) { |
| 2387 |
MOZ_ASSERT(type.isCanonicalValType()); |
| 2388 |
} |
| 2389 |
}; |
| 2390 |
|
| 2391 |
protected: |
| 2392 |
using LocalMap = |
| 2393 |
HashMap<TaggedParserAtomIndex, Local, TaggedParserAtomIndexHasher>; |
| 2394 |
using LabelMap = |
| 2395 |
HashMap<TaggedParserAtomIndex, uint32_t, TaggedParserAtomIndexHasher>; |
| 2396 |
|
| 2397 |
// This is also a ModuleValidator<Unit>& after the appropriate static_cast<>. |
| 2398 |
ModuleValidatorShared& m_; |
| 2399 |
|
| 2400 |
FunctionNode* fn_; |
| 2401 |
Bytes bytes_; |
| 2402 |
Encoder encoder_; |
| 2403 |
Uint32Vector callSiteLineNums_; |
| 2404 |
LocalMap locals_; |
| 2405 |
|
| 2406 |
// Labels |
| 2407 |
LabelMap breakLabels_; |
| 2408 |
LabelMap continueLabels_; |
| 2409 |
Uint32Vector breakableStack_; |
| 2410 |
Uint32Vector continuableStack_; |
| 2411 |
uint32_t blockDepth_; |
| 2412 |
|
| 2413 |
bool hasAlreadyReturned_; |
| 2414 |
Maybe<ValType> ret_; |
| 2415 |
|
| 2416 |
private: |
| 2417 |
FunctionValidatorShared(ModuleValidatorShared& m, FunctionNode* fn, |
| 2418 |
FrontendContext* fc) |
| 2419 |
: m_(m), |
| 2420 |
fn_(fn), |
| 2421 |
encoder_(bytes_), |
| 2422 |
locals_(fc), |
| 2423 |
breakLabels_(fc), |
| 2424 |
continueLabels_(fc), |
| 2425 |
blockDepth_(0), |
| 2426 |
hasAlreadyReturned_(false) {} |
| 2427 |
|
| 2428 |
protected: |
| 2429 |
template <typename Unit> |
| 2430 |
FunctionValidatorShared(ModuleValidator<Unit>& m, FunctionNode* fn, |
| 2431 |
FrontendContext* fc) |
| 2432 |
: FunctionValidatorShared(static_cast<ModuleValidatorShared&>(m), fn, |
| 2433 |
fc) {} |
| 2434 |
|
| 2435 |
public: |
| 2436 |
ModuleValidatorShared& m() const { return m_; } |
| 2437 |
|
| 2438 |
FrontendContext* fc() const { return m_.fc(); } |
| 2439 |
FunctionNode* fn() const { return fn_; } |
| 2440 |
|
| 2441 |
void define(ModuleValidatorShared::Func* func, unsigned line) { |
| 2442 |
MOZ_ASSERT(!blockDepth_); |
| 2443 |
MOZ_ASSERT(breakableStack_.empty()); |
| 2444 |
MOZ_ASSERT(continuableStack_.empty()); |
| 2445 |
MOZ_ASSERT(breakLabels_.empty()); |
| 2446 |
MOZ_ASSERT(continueLabels_.empty()); |
| 2447 |
func->define(fn_, line, std::move(bytes_), std::move(callSiteLineNums_)); |
| 2448 |
} |
| 2449 |
|
| 2450 |
bool fail(ParseNode* pn, const char* str) { return m_.fail(pn, str); } |
| 2451 |
|
| 2452 |
bool failf(ParseNode* pn, const char* fmt, ...) MOZ_FORMAT_PRINTF(3, 4) { |
| 2453 |
va_list ap; |
| 2454 |
va_start(ap, fmt); |
| 2455 |
m_.failfVAOffset(pn->pn_pos.begin, fmt, ap); |
| 2456 |
va_end(ap); |
| 2457 |
return false; |
| 2458 |
} |
| 2459 |
|
| 2460 |
bool failName(ParseNode* pn, const char* fmt, TaggedParserAtomIndex name) { |
| 2461 |
return m_.failName(pn, fmt, name); |
| 2462 |
} |
| 2463 |
|
| 2464 |
/***************************************************** Local scope setup */ |
| 2465 |
|
| 2466 |
bool addLocal(ParseNode* pn, TaggedParserAtomIndex name, Type type) { |
| 2467 |
LocalMap::AddPtr p = locals_.lookupForAdd(name); |
| 2468 |
if (p) { |
| 2469 |
return failName(pn, "duplicate local name '%s' not allowed", name); |
| 2470 |
} |
| 2471 |
return locals_.add(p, name, Local(type, locals_.count())); |
| 2472 |
} |
| 2473 |
|
| 2474 |
/****************************** For consistency of returns in a function */ |
| 2475 |
|
| 2476 |
bool hasAlreadyReturned() const { return hasAlreadyReturned_; } |
| 2477 |
|
| 2478 |
Maybe<ValType> returnedType() const { return ret_; } |
| 2479 |
|
| 2480 |
void setReturnedType(const Maybe<ValType>& ret) { |
| 2481 |
MOZ_ASSERT(!hasAlreadyReturned_); |
| 2482 |
ret_ = ret; |
| 2483 |
hasAlreadyReturned_ = true; |
| 2484 |
} |
| 2485 |
|
| 2486 |
/**************************************************************** Labels */ |
| 2487 |
private: |
| 2488 |
bool writeBr(uint32_t absolute, Op op = Op::Br) { |
| 2489 |
MOZ_ASSERT(op == Op::Br || op == Op::BrIf); |
| 2490 |
MOZ_ASSERT(absolute < blockDepth_); |
| 2491 |
return encoder().writeOp(op) && |
| 2492 |
encoder().writeVarU32(blockDepth_ - 1 - absolute); |
| 2493 |
} |
| 2494 |
void removeLabel(TaggedParserAtomIndex label, LabelMap* map) { |
| 2495 |
LabelMap::Ptr p = map->lookup(label); |
| 2496 |
MOZ_ASSERT(p); |
| 2497 |
map->remove(p); |
| 2498 |
} |
| 2499 |
|
| 2500 |
public: |
| 2501 |
bool pushBreakableBlock() { |
| 2502 |
return encoder().writeOp(Op::Block) && |
| 2503 |
encoder().writeFixedU8(uint8_t(TypeCode::BlockVoid)) && |
| 2504 |
breakableStack_.append(blockDepth_++); |
| 2505 |
} |
| 2506 |
bool popBreakableBlock() { |
| 2507 |
MOZ_ALWAYS_TRUE(breakableStack_.popCopy() == --blockDepth_); |
| 2508 |
return encoder().writeOp(Op::End); |
| 2509 |
} |
| 2510 |
|
| 2511 |
bool pushUnbreakableBlock(const LabelVector* labels = nullptr) { |
| 2512 |
if (labels) { |
| 2513 |
for (TaggedParserAtomIndex label : *labels) { |
| 2514 |
if (!breakLabels_.putNew(label, blockDepth_)) { |
| 2515 |
return false; |
| 2516 |
} |
| 2517 |
} |
| 2518 |
} |
| 2519 |
blockDepth_++; |
| 2520 |
return encoder().writeOp(Op::Block) && |
| 2521 |
encoder().writeFixedU8(uint8_t(TypeCode::BlockVoid)); |
| 2522 |
} |
| 2523 |
bool popUnbreakableBlock(const LabelVector* labels = nullptr) { |
| 2524 |
if (labels) { |
| 2525 |
for (TaggedParserAtomIndex label : *labels) { |
| 2526 |
removeLabel(label, &breakLabels_); |
| 2527 |
} |
| 2528 |
} |
| 2529 |
--blockDepth_; |
| 2530 |
return encoder().writeOp(Op::End); |
| 2531 |
} |
| 2532 |
|
| 2533 |
bool pushContinuableBlock() { |
| 2534 |
return encoder().writeOp(Op::Block) && |
| 2535 |
encoder().writeFixedU8(uint8_t(TypeCode::BlockVoid)) && |
| 2536 |
continuableStack_.append(blockDepth_++); |
| 2537 |
} |
| 2538 |
bool popContinuableBlock() { |
| 2539 |
MOZ_ALWAYS_TRUE(continuableStack_.popCopy() == --blockDepth_); |
| 2540 |
return encoder().writeOp(Op::End); |
| 2541 |
} |
| 2542 |
|
| 2543 |
bool pushLoop() { |
| 2544 |
return encoder().writeOp(Op::Block) && |
| 2545 |
encoder().writeFixedU8(uint8_t(TypeCode::BlockVoid)) && |
| 2546 |
encoder().writeOp(Op::Loop) && |
| 2547 |
encoder().writeFixedU8(uint8_t(TypeCode::BlockVoid)) && |
| 2548 |
breakableStack_.append(blockDepth_++) && |
| 2549 |
continuableStack_.append(blockDepth_++); |
| 2550 |
} |
| 2551 |
bool popLoop() { |
| 2552 |
MOZ_ALWAYS_TRUE(continuableStack_.popCopy() == --blockDepth_); |
| 2553 |
MOZ_ALWAYS_TRUE(breakableStack_.popCopy() == --blockDepth_); |
| 2554 |
return encoder().writeOp(Op::End) && encoder().writeOp(Op::End); |
| 2555 |
} |
| 2556 |
|
| 2557 |
bool pushIf(size_t* typeAt) { |
| 2558 |
++blockDepth_; |
| 2559 |
return encoder().writeOp(Op::If) && encoder().writePatchableFixedU7(typeAt); |
| 2560 |
} |
| 2561 |
bool switchToElse() { |
| 2562 |
MOZ_ASSERT(blockDepth_ > 0); |
| 2563 |
return encoder().writeOp(Op::Else); |
| 2564 |
} |
| 2565 |
void setIfType(size_t typeAt, TypeCode type) { |
| 2566 |
encoder().patchFixedU7(typeAt, uint8_t(type)); |
| 2567 |
} |
| 2568 |
bool popIf() { |
| 2569 |
MOZ_ASSERT(blockDepth_ > 0); |
| 2570 |
--blockDepth_; |
| 2571 |
return encoder().writeOp(Op::End); |
| 2572 |
} |
| 2573 |
bool popIf(size_t typeAt, TypeCode type) { |
| 2574 |
MOZ_ASSERT(blockDepth_ > 0); |
| 2575 |
--blockDepth_; |
| 2576 |
if (!encoder().writeOp(Op::End)) { |
| 2577 |
return false; |
| 2578 |
} |
| 2579 |
|
| 2580 |
setIfType(typeAt, type); |
| 2581 |
return true; |
| 2582 |
} |
| 2583 |
|
| 2584 |
bool writeBreakIf() { return writeBr(breakableStack_.back(), Op::BrIf); } |
| 2585 |
bool writeContinueIf() { return writeBr(continuableStack_.back(), Op::BrIf); } |
| 2586 |
bool writeUnlabeledBreakOrContinue(bool isBreak) { |
| 2587 |
return writeBr(isBreak ? breakableStack_.back() : continuableStack_.back()); |
| 2588 |
} |
| 2589 |
bool writeContinue() { return writeBr(continuableStack_.back()); } |
| 2590 |
|
| 2591 |
bool addLabels(const LabelVector& labels, uint32_t relativeBreakDepth, |
| 2592 |
uint32_t relativeContinueDepth) { |
| 2593 |
for (TaggedParserAtomIndex label : labels) { |
| 2594 |
if (!breakLabels_.putNew(label, blockDepth_ + relativeBreakDepth)) { |
| 2595 |
return false; |
| 2596 |
} |
| 2597 |
if (!continueLabels_.putNew(label, blockDepth_ + relativeContinueDepth)) { |
| 2598 |
return false; |
| 2599 |
} |
| 2600 |
} |
| 2601 |
return true; |
| 2602 |
} |
| 2603 |
void removeLabels(const LabelVector& labels) { |
| 2604 |
for (TaggedParserAtomIndex label : labels) { |
| 2605 |
removeLabel(label, &breakLabels_); |
| 2606 |
removeLabel(label, &continueLabels_); |
| 2607 |
} |
| 2608 |
} |
| 2609 |
bool writeLabeledBreakOrContinue(TaggedParserAtomIndex label, bool isBreak) { |
| 2610 |
LabelMap& map = isBreak ? breakLabels_ : continueLabels_; |
| 2611 |
if (LabelMap::Ptr p = map.lookup(label)) { |
| 2612 |
return writeBr(p->value()); |
| 2613 |
} |
| 2614 |
MOZ_CRASH("nonexistent label"); |
| 2615 |
} |
| 2616 |
|
| 2617 |
/*************************************************** Read-only interface */ |
| 2618 |
|
| 2619 |
const Local* lookupLocal(TaggedParserAtomIndex name) const { |
| 2620 |
if (auto p = locals_.lookup(name)) { |
| 2621 |
return &p->value(); |
| 2622 |
} |
| 2623 |
return nullptr; |
| 2624 |
} |
| 2625 |
|
| 2626 |
const ModuleValidatorShared::Global* lookupGlobal( |
| 2627 |
TaggedParserAtomIndex name) const { |
| 2628 |
if (locals_.has(name)) { |
| 2629 |
return nullptr; |
| 2630 |
} |
| 2631 |
return m_.lookupGlobal(name); |
| 2632 |
} |
| 2633 |
|
| 2634 |
size_t numLocals() const { return locals_.count(); } |
| 2635 |
|
| 2636 |
/**************************************************** Encoding interface */ |
| 2637 |
|
| 2638 |
Encoder& encoder() { return encoder_; } |
| 2639 |
|
| 2640 |
[[nodiscard]] bool writeInt32Lit(int32_t i32) { |
| 2641 |
return encoder().writeOp(Op::I32Const) && encoder().writeVarS32(i32); |
| 2642 |
} |
| 2643 |
[[nodiscard]] bool writeConstExpr(const NumLit& lit) { |
| 2644 |
switch (lit.which()) { |
| 2645 |
case NumLit::Fixnum: |
| 2646 |
case NumLit::NegativeInt: |
| 2647 |
case NumLit::BigUnsigned: |
| 2648 |
return writeInt32Lit(lit.toInt32()); |
| 2649 |
case NumLit::Float: |
| 2650 |
return encoder().writeOp(Op::F32Const) && |
| 2651 |
encoder().writeFixedF32(lit.toFloat()); |
| 2652 |
case NumLit::Double: |
| 2653 |
return encoder().writeOp(Op::F64Const) && |
| 2654 |
encoder().writeFixedF64(lit.toDouble()); |
| 2655 |
case NumLit::OutOfRangeInt: |
| 2656 |
break; |
| 2657 |
} |
| 2658 |
MOZ_CRASH("unexpected literal type"); |
| 2659 |
} |
| 2660 |
}; |
| 2661 |
|
| 2662 |
// Encapsulates the building of an asm bytecode function from an asm.js function |
| 2663 |
// source code, packing the asm.js code into the asm bytecode form that can |
| 2664 |
// be decoded and compiled with a FunctionCompiler. |
| 2665 |
template <typename Unit> |
| 2666 |
class MOZ_STACK_CLASS FunctionValidator : public FunctionValidatorShared { |
| 2667 |
public: |
| 2668 |
FunctionValidator(ModuleValidator<Unit>& m, FunctionNode* fn) |
| 2669 |
: FunctionValidatorShared(m, fn, m.fc()) {} |
| 2670 |
|
| 2671 |
public: |
| 2672 |
ModuleValidator<Unit>& m() const { |
| 2673 |
return static_cast<ModuleValidator<Unit>&>(FunctionValidatorShared::m()); |
| 2674 |
} |
| 2675 |
|
| 2676 |
[[nodiscard]] bool writeCall(ParseNode* pn, Op op) { |
| 2677 |
MOZ_ASSERT(op == Op::Call); |
| 2678 |
if (!encoder().writeOp(op)) { |
| 2679 |
return false; |
| 2680 |
} |
| 2681 |
|
| 2682 |
return appendCallSiteLineNumber(pn); |
| 2683 |
} |
| 2684 |
[[nodiscard]] bool writeCall(ParseNode* pn, MozOp op) { |
| 2685 |
MOZ_ASSERT(op == MozOp::OldCallDirect || op == MozOp::OldCallIndirect); |
| 2686 |
if (!encoder().writeOp(op)) { |
| 2687 |
return false; |
| 2688 |
} |
| 2689 |
|
| 2690 |
return appendCallSiteLineNumber(pn); |
| 2691 |
} |
| 2692 |
[[nodiscard]] bool prepareCall(ParseNode* pn) { |
| 2693 |
return appendCallSiteLineNumber(pn); |
| 2694 |
} |
| 2695 |
|
| 2696 |
private: |
| 2697 |
[[nodiscard]] bool appendCallSiteLineNumber(ParseNode* node) { |
| 2698 |
const TokenStreamAnyChars& anyChars = m().tokenStream().anyCharsAccess(); |
| 2699 |
auto lineToken = anyChars.lineToken(node->pn_pos.begin); |
| 2700 |
uint32_t lineNumber = anyChars.lineNumber(lineToken); |
| 2701 |
if (lineNumber > CallSiteDesc::MAX_LINE_OR_BYTECODE_VALUE) { |
| 2702 |
return fail(node, "line number exceeding implementation limits"); |
| 2703 |
} |
| 2704 |
return callSiteLineNums_.append(lineNumber); |
| 2705 |
} |
| 2706 |
}; |
| 2707 |
|
| 2708 |
} /* anonymous namespace */ |
| 2709 |
|
| 2710 |
/*****************************************************************************/ |
| 2711 |
// asm.js type-checking and code-generation algorithm |
| 2712 |
|
| 2713 |
static bool CheckIdentifier(ModuleValidatorShared& m, ParseNode* usepn, |
| 2714 |
TaggedParserAtomIndex name) { |
| 2715 |
if (name == TaggedParserAtomIndex::WellKnown::arguments() || |
| 2716 |
name == TaggedParserAtomIndex::WellKnown::eval()) { |
| 2717 |
return m.failName(usepn, "'%s' is not an allowed identifier", name); |
| 2718 |
} |
| 2719 |
return true; |
| 2720 |
} |
| 2721 |
|
| 2722 |
static bool CheckModuleLevelName(ModuleValidatorShared& m, ParseNode* usepn, |
| 2723 |
TaggedParserAtomIndex name) { |
| 2724 |
if (!CheckIdentifier(m, usepn, name)) { |
| 2725 |
return false; |
| 2726 |
} |
| 2727 |
|
| 2728 |
if (name == m.moduleFunctionName() || name == m.globalArgumentName() || |
| 2729 |
name == m.importArgumentName() || name == m.bufferArgumentName() || |
| 2730 |
m.lookupGlobal(name)) { |
| 2731 |
return m.failName(usepn, "duplicate name '%s' not allowed", name); |
| 2732 |
} |
| 2733 |
|
| 2734 |
return true; |
| 2735 |
} |
| 2736 |
|
| 2737 |
static bool CheckFunctionHead(ModuleValidatorShared& m, FunctionNode* funNode) { |
| 2738 |
FunctionBox* funbox = funNode->funbox(); |
| 2739 |
MOZ_ASSERT(!funbox->hasExprBody()); |
| 2740 |
|
| 2741 |
if (funbox->hasRest()) { |
| 2742 |
return m.fail(funNode, "rest args not allowed"); |
| 2743 |
} |
| 2744 |
if (funbox->hasDestructuringArgs) { |
| 2745 |
return m.fail(funNode, "destructuring args not allowed"); |
| 2746 |
} |
| 2747 |
return true; |
| 2748 |
} |
| 2749 |
|
| 2750 |
static bool CheckArgument(ModuleValidatorShared& m, ParseNode* arg, |
| 2751 |
TaggedParserAtomIndex* name) { |
| 2752 |
*name = TaggedParserAtomIndex::null(); |
| 2753 |
|
| 2754 |
if (!arg->isKind(ParseNodeKind::Name)) { |
| 2755 |
return m.fail(arg, "argument is not a plain name"); |
| 2756 |
} |
| 2757 |
|
| 2758 |
TaggedParserAtomIndex argName = arg->as<NameNode>().name(); |
| 2759 |
if (!CheckIdentifier(m, arg, argName)) { |
| 2760 |
return false; |
| 2761 |
} |
| 2762 |
|
| 2763 |
*name = argName; |
| 2764 |
return true; |
| 2765 |
} |
| 2766 |
|
| 2767 |
static bool CheckModuleArgument(ModuleValidatorShared& m, ParseNode* arg, |
| 2768 |
TaggedParserAtomIndex* name) { |
| 2769 |
if (!CheckArgument(m, arg, name)) { |
| 2770 |
return false; |
| 2771 |
} |
| 2772 |
|
| 2773 |
return CheckModuleLevelName(m, arg, *name); |
| 2774 |
} |
| 2775 |
|
| 2776 |
static bool CheckModuleArguments(ModuleValidatorShared& m, |
| 2777 |
FunctionNode* funNode) { |
| 2778 |
unsigned numFormals; |
| 2779 |
ParseNode* arg1 = FunctionFormalParametersList(funNode, &numFormals); |
| 2780 |
ParseNode* arg2 = arg1 ? NextNode(arg1) : nullptr; |
| 2781 |
ParseNode* arg3 = arg2 ? NextNode(arg2) : nullptr; |
| 2782 |
|
| 2783 |
if (numFormals > 3) { |
| 2784 |
return m.fail(funNode, "asm.js modules takes at most 3 argument"); |
| 2785 |
} |
| 2786 |
|
| 2787 |
TaggedParserAtomIndex arg1Name; |
| 2788 |
if (arg1 && !CheckModuleArgument(m, arg1, &arg1Name)) { |
| 2789 |
return false; |
| 2790 |
} |
| 2791 |
if (!m.initGlobalArgumentName(arg1Name)) { |
| 2792 |
return false; |
| 2793 |
} |
| 2794 |
|
| 2795 |
TaggedParserAtomIndex arg2Name; |
| 2796 |
if (arg2 && !CheckModuleArgument(m, arg2, &arg2Name)) { |
| 2797 |
return false; |
| 2798 |
} |
| 2799 |
if (!m.initImportArgumentName(arg2Name)) { |
| 2800 |
return false; |
| 2801 |
} |
| 2802 |
|
| 2803 |
TaggedParserAtomIndex arg3Name; |
| 2804 |
if (arg3 && !CheckModuleArgument(m, arg3, &arg3Name)) { |
| 2805 |
return false; |
| 2806 |
} |
| 2807 |
return m.initBufferArgumentName(arg3Name); |
| 2808 |
} |
| 2809 |
|
| 2810 |
static bool CheckPrecedingStatements(ModuleValidatorShared& m, |
| 2811 |
ParseNode* stmtList) { |
| 2812 |
MOZ_ASSERT(stmtList->isKind(ParseNodeKind::StatementList)); |
| 2813 |
|
| 2814 |
ParseNode* stmt = ListHead(stmtList); |
| 2815 |
for (unsigned i = 0, n = ListLength(stmtList); i < n; i++) { |
| 2816 |
if (!IsIgnoredDirective(stmt)) { |
| 2817 |
return m.fail(stmt, "invalid asm.js statement"); |
| 2818 |
} |
| 2819 |
} |
| 2820 |
|
| 2821 |
return true; |
| 2822 |
} |
| 2823 |
|
| 2824 |
static bool CheckGlobalVariableInitConstant(ModuleValidatorShared& m, |
| 2825 |
TaggedParserAtomIndex varName, |
| 2826 |
ParseNode* initNode, bool isConst) { |
| 2827 |
NumLit lit = ExtractNumericLiteral(m, initNode); |
| 2828 |
if (!lit.valid()) { |
| 2829 |
return m.fail(initNode, |
| 2830 |
"global initializer is out of representable integer range"); |
| 2831 |
} |
| 2832 |
|
| 2833 |
Type canonicalType = Type::canonicalize(Type::lit(lit)); |
| 2834 |
if (!canonicalType.isGlobalVarType()) { |
| 2835 |
return m.fail(initNode, "global variable type not allowed"); |
| 2836 |
} |
| 2837 |
|
| 2838 |
return m.addGlobalVarInit(varName, lit, canonicalType, isConst); |
| 2839 |
} |
| 2840 |
|
| 2841 |
static bool CheckTypeAnnotation(ModuleValidatorShared& m, |
| 2842 |
ParseNode* coercionNode, Type* coerceTo, |
| 2843 |
ParseNode** coercedExpr = nullptr) { |
| 2844 |
switch (coercionNode->getKind()) { |
| 2845 |
case ParseNodeKind::BitOrExpr: { |
| 2846 |
ParseNode* rhs = BitwiseRight(coercionNode); |
| 2847 |
uint32_t i; |
| 2848 |
if (!IsLiteralInt(m, rhs, &i) || i != 0) { |
| 2849 |
return m.fail(rhs, "must use |0 for argument/return coercion"); |
| 2850 |
} |
| 2851 |
*coerceTo = Type::Int; |
| 2852 |
if (coercedExpr) { |
| 2853 |
*coercedExpr = BitwiseLeft(coercionNode); |
| 2854 |
} |
| 2855 |
return true; |
| 2856 |
} |
| 2857 |
case ParseNodeKind::PosExpr: { |
| 2858 |
*coerceTo = Type::Double; |
| 2859 |
if (coercedExpr) { |
| 2860 |
*coercedExpr = UnaryKid(coercionNode); |
| 2861 |
} |
| 2862 |
return true; |
| 2863 |
} |
| 2864 |
case ParseNodeKind::CallExpr: { |
| 2865 |
if (IsCoercionCall(m, coercionNode, coerceTo, coercedExpr)) { |
| 2866 |
return true; |
| 2867 |
} |
| 2868 |
break; |
| 2869 |
} |
| 2870 |
default:; |
| 2871 |
} |
| 2872 |
|
| 2873 |
return m.fail(coercionNode, "must be of the form +x, x|0 or fround(x)"); |
| 2874 |
} |
| 2875 |
|
| 2876 |
static bool CheckGlobalVariableInitImport(ModuleValidatorShared& m, |
| 2877 |
TaggedParserAtomIndex varName, |
| 2878 |
ParseNode* initNode, bool isConst) { |
| 2879 |
Type coerceTo; |
| 2880 |
ParseNode* coercedExpr; |
| 2881 |
if (!CheckTypeAnnotation(m, initNode, &coerceTo, &coercedExpr)) { |
| 2882 |
return false; |
| 2883 |
} |
| 2884 |
|
| 2885 |
if (!coercedExpr->isKind(ParseNodeKind::DotExpr)) { |
| 2886 |
return m.failName(coercedExpr, "invalid import expression for global '%s'", |
| 2887 |
varName); |
| 2888 |
} |
| 2889 |
|
| 2890 |
if (!coerceTo.isGlobalVarType()) { |
| 2891 |
return m.fail(initNode, "global variable type not allowed"); |
| 2892 |
} |
| 2893 |
|
| 2894 |
ParseNode* base = DotBase(coercedExpr); |
| 2895 |
TaggedParserAtomIndex field = DotMember(coercedExpr); |
| 2896 |
|
| 2897 |
TaggedParserAtomIndex importName = m.importArgumentName(); |
| 2898 |
if (!importName) { |
| 2899 |
return m.fail(coercedExpr, |
| 2900 |
"cannot import without an asm.js foreign parameter"); |
| 2901 |
} |
| 2902 |
if (!IsUseOfName(base, importName)) { |
| 2903 |
return m.failName(coercedExpr, "base of import expression must be '%s'", |
| 2904 |
importName); |
| 2905 |
} |
| 2906 |
|
| 2907 |
return m.addGlobalVarImport(varName, field, coerceTo, isConst); |
| 2908 |
} |
| 2909 |
|
| 2910 |
static bool IsArrayViewCtorName(ModuleValidatorShared& m, |
| 2911 |
TaggedParserAtomIndex name, |
| 2912 |
Scalar::Type* type) { |
| 2913 |
if (name == TaggedParserAtomIndex::WellKnown::Int8Array()) { |
| 2914 |
*type = Scalar::Int8; |
| 2915 |
} else if (name == TaggedParserAtomIndex::WellKnown::Uint8Array()) { |
| 2916 |
*type = Scalar::Uint8; |
| 2917 |
} else if (name == TaggedParserAtomIndex::WellKnown::Int16Array()) { |
| 2918 |
*type = Scalar::Int16; |
| 2919 |
} else if (name == TaggedParserAtomIndex::WellKnown::Uint16Array()) { |
| 2920 |
*type = Scalar::Uint16; |
| 2921 |
} else if (name == TaggedParserAtomIndex::WellKnown::Int32Array()) { |
| 2922 |
*type = Scalar::Int32; |
| 2923 |
} else if (name == TaggedParserAtomIndex::WellKnown::Uint32Array()) { |
| 2924 |
*type = Scalar::Uint32; |
| 2925 |
} else if (name == TaggedParserAtomIndex::WellKnown::Float32Array()) { |
| 2926 |
*type = Scalar::Float32; |
| 2927 |
} else if (name == TaggedParserAtomIndex::WellKnown::Float64Array()) { |
| 2928 |
*type = Scalar::Float64; |
| 2929 |
} else { |
| 2930 |
return false; |
| 2931 |
} |
| 2932 |
return true; |
| 2933 |
} |
| 2934 |
|
| 2935 |
static bool CheckNewArrayViewArgs(ModuleValidatorShared& m, ParseNode* newExpr, |
| 2936 |
TaggedParserAtomIndex bufferName) { |
| 2937 |
ParseNode* ctorExpr = BinaryLeft(newExpr); |
| 2938 |
ParseNode* ctorArgs = BinaryRight(newExpr); |
| 2939 |
ParseNode* bufArg = ListHead(ctorArgs); |
| 2940 |
if (!bufArg || NextNode(bufArg) != nullptr) { |
| 2941 |
return m.fail(ctorExpr, |
| 2942 |
"array view constructor takes exactly one argument"); |
| 2943 |
} |
| 2944 |
|
| 2945 |
if (!IsUseOfName(bufArg, bufferName)) { |
| 2946 |
return m.failName(bufArg, "argument to array view constructor must be '%s'", |
| 2947 |
bufferName); |
| 2948 |
} |
| 2949 |
|
| 2950 |
return true; |
| 2951 |
} |
| 2952 |
|
| 2953 |
static bool CheckNewArrayView(ModuleValidatorShared& m, |
| 2954 |
TaggedParserAtomIndex varName, |
| 2955 |
ParseNode* newExpr) { |
| 2956 |
TaggedParserAtomIndex globalName = m.globalArgumentName(); |
| 2957 |
if (!globalName) { |
| 2958 |
return m.fail( |
| 2959 |
newExpr, "cannot create array view without an asm.js global parameter"); |
| 2960 |
} |
| 2961 |
|
| 2962 |
TaggedParserAtomIndex bufferName = m.bufferArgumentName(); |
| 2963 |
if (!bufferName) { |
| 2964 |
return m.fail(newExpr, |
| 2965 |
"cannot create array view without an asm.js heap parameter"); |
| 2966 |
} |
| 2967 |
|
| 2968 |
ParseNode* ctorExpr = BinaryLeft(newExpr); |
| 2969 |
|
| 2970 |
TaggedParserAtomIndex field; |
| 2971 |
Scalar::Type type; |
| 2972 |
if (ctorExpr->isKind(ParseNodeKind::DotExpr)) { |
| 2973 |
ParseNode* base = DotBase(ctorExpr); |
| 2974 |
|
| 2975 |
if (!IsUseOfName(base, globalName)) { |
| 2976 |
return m.failName(base, "expecting '%s.*Array", globalName); |
| 2977 |
} |
| 2978 |
|
| 2979 |
field = DotMember(ctorExpr); |
| 2980 |
if (!IsArrayViewCtorName(m, field, &type)) { |
| 2981 |
return m.fail(ctorExpr, "could not match typed array name"); |
| 2982 |
} |
| 2983 |
} else { |
| 2984 |
if (!ctorExpr->isKind(ParseNodeKind::Name)) { |
| 2985 |
return m.fail(ctorExpr, |
| 2986 |
"expecting name of imported array view constructor"); |
| 2987 |
} |
| 2988 |
|
| 2989 |
TaggedParserAtomIndex globalName = ctorExpr->as<NameNode>().name(); |
| 2990 |
const ModuleValidatorShared::Global* global = m.lookupGlobal(globalName); |
| 2991 |
if (!global) { |
| 2992 |
return m.failName(ctorExpr, "%s not found in module global scope", |
| 2993 |
globalName); |
| 2994 |
} |
| 2995 |
|
| 2996 |
if (global->which() != ModuleValidatorShared::Global::ArrayViewCtor) { |
| 2997 |
return m.failName(ctorExpr, |
| 2998 |
"%s must be an imported array view constructor", |
| 2999 |
globalName); |
| 3000 |
} |
| 3001 |
|
| 3002 |
type = global->viewType(); |
| 3003 |
} |
| 3004 |
|
| 3005 |
if (!CheckNewArrayViewArgs(m, newExpr, bufferName)) { |
| 3006 |
return false; |
| 3007 |
} |
| 3008 |
|
| 3009 |
return m.addArrayView(varName, type, field); |
| 3010 |
} |
| 3011 |
|
| 3012 |
static bool CheckGlobalMathImport(ModuleValidatorShared& m, ParseNode* initNode, |
| 3013 |
TaggedParserAtomIndex varName, |
| 3014 |
TaggedParserAtomIndex field) { |
| 3015 |
// Math builtin, with the form glob.Math.[[builtin]] |
| 3016 |
ModuleValidatorShared::MathBuiltin mathBuiltin; |
| 3017 |
if (!m.lookupStandardLibraryMathName(field, &mathBuiltin)) { |
| 3018 |
return m.failName(initNode, "'%s' is not a standard Math builtin", field); |
| 3019 |
} |
| 3020 |
|
| 3021 |
switch (mathBuiltin.kind) { |
| 3022 |
case ModuleValidatorShared::MathBuiltin::Function: |
| 3023 |
return m.addMathBuiltinFunction(varName, mathBuiltin.u.func, field); |
| 3024 |
case ModuleValidatorShared::MathBuiltin::Constant: |
| 3025 |
return m.addMathBuiltinConstant(varName, mathBuiltin.u.cst, field); |
| 3026 |
default: |
| 3027 |
break; |
| 3028 |
} |
| 3029 |
MOZ_CRASH("unexpected or uninitialized math builtin type"); |
| 3030 |
} |
| 3031 |
|
| 3032 |
static bool CheckGlobalDotImport(ModuleValidatorShared& m, |
| 3033 |
TaggedParserAtomIndex varName, |
| 3034 |
ParseNode* initNode) { |
| 3035 |
ParseNode* base = DotBase(initNode); |
| 3036 |
TaggedParserAtomIndex field = DotMember(initNode); |
| 3037 |
|
| 3038 |
if (base->isKind(ParseNodeKind::DotExpr)) { |
| 3039 |
ParseNode* global = DotBase(base); |
| 3040 |
TaggedParserAtomIndex math = DotMember(base); |
| 3041 |
|
| 3042 |
TaggedParserAtomIndex globalName = m.globalArgumentName(); |
| 3043 |
if (!globalName) { |
| 3044 |
return m.fail( |
| 3045 |
base, "import statement requires the module have a stdlib parameter"); |
| 3046 |
} |
| 3047 |
|
| 3048 |
if (!IsUseOfName(global, globalName)) { |
| 3049 |
if (global->isKind(ParseNodeKind::DotExpr)) { |
| 3050 |
return m.failName(base, |
| 3051 |
"imports can have at most two dot accesses " |
| 3052 |
"(e.g. %s.Math.sin)", |
| 3053 |
globalName); |
| 3054 |
} |
| 3055 |
return m.failName(base, "expecting %s.*", globalName); |
| 3056 |
} |
| 3057 |
|
| 3058 |
if (math == TaggedParserAtomIndex::WellKnown::Math()) { |
| 3059 |
return CheckGlobalMathImport(m, initNode, varName, field); |
| 3060 |
} |
| 3061 |
return m.failName(base, "expecting %s.Math", globalName); |
| 3062 |
} |
| 3063 |
|
| 3064 |
if (!base->isKind(ParseNodeKind::Name)) { |
| 3065 |
return m.fail(base, "expected name of variable or parameter"); |
| 3066 |
} |
| 3067 |
|
| 3068 |
auto baseName = base->as<NameNode>().name(); |
| 3069 |
if (baseName == m.globalArgumentName()) { |
| 3070 |
if (field == TaggedParserAtomIndex::WellKnown::NaN()) { |
| 3071 |
return m.addGlobalConstant(varName, GenericNaN(), field); |
| 3072 |
} |
| 3073 |
if (field == TaggedParserAtomIndex::WellKnown::Infinity()) { |
| 3074 |
return m.addGlobalConstant(varName, PositiveInfinity<double>(), field); |
| 3075 |
} |
| 3076 |
|
| 3077 |
Scalar::Type type; |
| 3078 |
if (IsArrayViewCtorName(m, field, &type)) { |
| 3079 |
return m.addArrayViewCtor(varName, type, field); |
| 3080 |
} |
| 3081 |
|
| 3082 |
return m.failName( |
| 3083 |
initNode, "'%s' is not a standard constant or typed array name", field); |
| 3084 |
} |
| 3085 |
|
| 3086 |
if (baseName != m.importArgumentName()) { |
| 3087 |
return m.fail(base, "expected global or import name"); |
| 3088 |
} |
| 3089 |
|
| 3090 |
return m.addFFI(varName, field); |
| 3091 |
} |
| 3092 |
|
| 3093 |
static bool CheckModuleGlobal(ModuleValidatorShared& m, ParseNode* decl, |
| 3094 |
bool isConst) { |
| 3095 |
if (!decl->isKind(ParseNodeKind::AssignExpr)) { |
| 3096 |
return m.fail(decl, "module import needs initializer"); |
| 3097 |
} |
| 3098 |
AssignmentNode* assignNode = &decl->as<AssignmentNode>(); |
| 3099 |
|
| 3100 |
ParseNode* var = assignNode->left(); |
| 3101 |
|
| 3102 |
if (!var->isKind(ParseNodeKind::Name)) { |
| 3103 |
return m.fail(var, "import variable is not a plain name"); |
| 3104 |
} |
| 3105 |
|
| 3106 |
TaggedParserAtomIndex varName = var->as<NameNode>().name(); |
| 3107 |
if (!CheckModuleLevelName(m, var, varName)) { |
| 3108 |
return false; |
| 3109 |
} |
| 3110 |
|
| 3111 |
ParseNode* initNode = assignNode->right(); |
| 3112 |
|
| 3113 |
if (IsNumericLiteral(m, initNode)) { |
| 3114 |
return CheckGlobalVariableInitConstant(m, varName, initNode, isConst); |
| 3115 |
} |
| 3116 |
|
| 3117 |
if (initNode->isKind(ParseNodeKind::BitOrExpr) || |
| 3118 |
initNode->isKind(ParseNodeKind::PosExpr) || |
| 3119 |
initNode->isKind(ParseNodeKind::CallExpr)) { |
| 3120 |
return CheckGlobalVariableInitImport(m, varName, initNode, isConst); |
| 3121 |
} |
| 3122 |
|
| 3123 |
if (initNode->isKind(ParseNodeKind::NewExpr)) { |
| 3124 |
return CheckNewArrayView(m, varName, initNode); |
| 3125 |
} |
| 3126 |
|
| 3127 |
if (initNode->isKind(ParseNodeKind::DotExpr)) { |
| 3128 |
return CheckGlobalDotImport(m, varName, initNode); |
| 3129 |
} |
| 3130 |
|
| 3131 |
return m.fail(initNode, "unsupported import expression"); |
| 3132 |
} |
| 3133 |
|
| 3134 |
template <typename Unit> |
| 3135 |
static bool CheckModuleProcessingDirectives(ModuleValidator<Unit>& m) { |
| 3136 |
auto& ts = m.parser().tokenStream; |
| 3137 |
while (true) { |
| 3138 |
bool matched; |
| 3139 |
if (!ts.matchToken(&matched, TokenKind::String, |
| 3140 |
TokenStreamShared::SlashIsRegExp)) { |
| 3141 |
return false; |
| 3142 |
} |
| 3143 |
if (!matched) { |
| 3144 |
return true; |
| 3145 |
} |
| 3146 |
|
| 3147 |
if (!IsIgnoredDirectiveName(ts.anyCharsAccess().currentToken().atom())) { |
| 3148 |
return m.failCurrentOffset("unsupported processing directive"); |
| 3149 |
} |
| 3150 |
|
| 3151 |
TokenKind tt; |
| 3152 |
if (!ts.getToken(&tt)) { |
| 3153 |
return false; |
| 3154 |
} |
| 3155 |
if (tt != TokenKind::Semi) { |
| 3156 |
return m.failCurrentOffset("expected semicolon after string literal"); |
| 3157 |
} |
| 3158 |
} |
| 3159 |
} |
| 3160 |
|
| 3161 |
template <typename Unit> |
| 3162 |
static bool CheckModuleGlobals(ModuleValidator<Unit>& m) { |
| 3163 |
while (true) { |
| 3164 |
ParseNode* varStmt; |
| 3165 |
if (!ParseVarOrConstStatement(m.parser(), &varStmt)) { |
| 3166 |
return false; |
| 3167 |
} |
| 3168 |
if (!varStmt) { |
| 3169 |
break; |
| 3170 |
} |
| 3171 |
for (ParseNode* var = VarListHead(varStmt); var; var = NextNode(var)) { |
| 3172 |
if (!CheckModuleGlobal(m, var, |
| 3173 |
varStmt->isKind(ParseNodeKind::ConstDecl))) { |
| 3174 |
return false; |
| 3175 |
} |
| 3176 |
} |
| 3177 |
} |
| 3178 |
|
| 3179 |
return true; |
| 3180 |
} |
| 3181 |
|
| 3182 |
static bool ArgFail(FunctionValidatorShared& f, TaggedParserAtomIndex argName, |
| 3183 |
ParseNode* stmt) { |
| 3184 |
return f.failName(stmt, |
| 3185 |
"expecting argument type declaration for '%s' of the " |
| 3186 |
"form 'arg = arg|0' or 'arg = +arg' or 'arg = fround(arg)'", |
| 3187 |
argName); |
| 3188 |
} |
| 3189 |
|
| 3190 |
static bool CheckArgumentType(FunctionValidatorShared& f, ParseNode* stmt, |
| 3191 |
TaggedParserAtomIndex name, Type* type) { |
| 3192 |
if (!stmt || !IsExpressionStatement(stmt)) { |
| 3193 |
return ArgFail(f, name, stmt ? stmt : f.fn()); |
| 3194 |
} |
| 3195 |
|
| 3196 |
ParseNode* initNode = ExpressionStatementExpr(stmt); |
| 3197 |
if (!initNode->isKind(ParseNodeKind::AssignExpr)) { |
| 3198 |
return ArgFail(f, name, stmt); |
| 3199 |
} |
| 3200 |
|
| 3201 |
ParseNode* argNode = BinaryLeft(initNode); |
| 3202 |
ParseNode* coercionNode = BinaryRight(initNode); |
| 3203 |
|
| 3204 |
if (!IsUseOfName(argNode, name)) { |
| 3205 |
return ArgFail(f, name, stmt); |
| 3206 |
} |
| 3207 |
|
| 3208 |
ParseNode* coercedExpr; |
| 3209 |
if (!CheckTypeAnnotation(f.m(), coercionNode, type, &coercedExpr)) { |
| 3210 |
return false; |
| 3211 |
} |
| 3212 |
|
| 3213 |
if (!type->isArgType()) { |
| 3214 |
return f.failName(stmt, "invalid type for argument '%s'", name); |
| 3215 |
} |
| 3216 |
|
| 3217 |
if (!IsUseOfName(coercedExpr, name)) { |
| 3218 |
return ArgFail(f, name, stmt); |
| 3219 |
} |
| 3220 |
|
| 3221 |
return true; |
| 3222 |
} |
| 3223 |
|
| 3224 |
static bool CheckProcessingDirectives(ModuleValidatorShared& m, |
| 3225 |
ParseNode** stmtIter) { |
| 3226 |
ParseNode* stmt = *stmtIter; |
| 3227 |
|
| 3228 |
while (stmt && IsIgnoredDirective(stmt)) { |
| 3229 |
stmt = NextNode(stmt); |
| 3230 |
} |
| 3231 |
|
| 3232 |
*stmtIter = stmt; |
| 3233 |
return true; |
| 3234 |
} |
| 3235 |
|
| 3236 |
static bool CheckArguments(FunctionValidatorShared& f, ParseNode** stmtIter, |
| 3237 |
ValTypeVector* argTypes) { |
| 3238 |
ParseNode* stmt = *stmtIter; |
| 3239 |
|
| 3240 |
unsigned numFormals; |
| 3241 |
ParseNode* argpn = FunctionFormalParametersList(f.fn(), &numFormals); |
| 3242 |
|
| 3243 |
for (unsigned i = 0; i < numFormals; |
| 3244 |
i++, argpn = NextNode(argpn), stmt = NextNode(stmt)) { |
| 3245 |
TaggedParserAtomIndex name; |
| 3246 |
if (!CheckArgument(f.m(), argpn, &name)) { |
| 3247 |
return false; |
| 3248 |
} |
| 3249 |
|
| 3250 |
Type type; |
| 3251 |
if (!CheckArgumentType(f, stmt, name, &type)) { |
| 3252 |
return false; |
| 3253 |
} |
| 3254 |
|
| 3255 |
if (!argTypes->append(type.canonicalToValType())) { |
| 3256 |
return false; |
| 3257 |
} |
| 3258 |
|
| 3259 |
if (!f.addLocal(argpn, name, type)) { |
| 3260 |
return false; |
| 3261 |
} |
| 3262 |
} |
| 3263 |
|
| 3264 |
if (argTypes->length() > MaxParams) { |
| 3265 |
return f.fail(stmt, "too many parameters"); |
| 3266 |
} |
| 3267 |
|
| 3268 |
*stmtIter = stmt; |
| 3269 |
return true; |
| 3270 |
} |
| 3271 |
|
| 3272 |
static bool IsLiteralOrConst(FunctionValidatorShared& f, ParseNode* pn, |
| 3273 |
NumLit* lit) { |
| 3274 |
if (pn->isKind(ParseNodeKind::Name)) { |
| 3275 |
const ModuleValidatorShared::Global* global = |
| 3276 |
f.lookupGlobal(pn->as<NameNode>().name()); |
| 3277 |
if (!global || |
| 3278 |
global->which() != ModuleValidatorShared::Global::ConstantLiteral) { |
| 3279 |
return false; |
| 3280 |
} |
| 3281 |
|
| 3282 |
*lit = global->constLiteralValue(); |
| 3283 |
return true; |
| 3284 |
} |
| 3285 |
|
| 3286 |
if (!IsNumericLiteral(f.m(), pn)) { |
| 3287 |
return false; |
| 3288 |
} |
| 3289 |
|
| 3290 |
*lit = ExtractNumericLiteral(f.m(), pn); |
| 3291 |
return true; |
| 3292 |
} |
| 3293 |
|
| 3294 |
static bool CheckFinalReturn(FunctionValidatorShared& f, |
| 3295 |
ParseNode* lastNonEmptyStmt) { |
| 3296 |
if (!f.encoder().writeOp(Op::End)) { |
| 3297 |
return false; |
| 3298 |
} |
| 3299 |
|
| 3300 |
if (!f.hasAlreadyReturned()) { |
| 3301 |
f.setReturnedType(Nothing()); |
| 3302 |
return true; |
| 3303 |
} |
| 3304 |
|
| 3305 |
if (!lastNonEmptyStmt->isKind(ParseNodeKind::ReturnStmt) && |
| 3306 |
f.returnedType()) { |
| 3307 |
return f.fail(lastNonEmptyStmt, |
| 3308 |
"void incompatible with previous return type"); |
| 3309 |
} |
| 3310 |
|
| 3311 |
return true; |
| 3312 |
} |
| 3313 |
|
| 3314 |
static bool CheckVariable(FunctionValidatorShared& f, ParseNode* decl, |
| 3315 |
ValTypeVector* types, Vector<NumLit>* inits) { |
| 3316 |
if (!decl->isKind(ParseNodeKind::AssignExpr)) { |
| 3317 |
return f.failName( |
| 3318 |
decl, "var '%s' needs explicit type declaration via an initial value", |
| 3319 |
decl->as<NameNode>().name()); |
| 3320 |
} |
| 3321 |
AssignmentNode* assignNode = &decl->as<AssignmentNode>(); |
| 3322 |
|
| 3323 |
ParseNode* var = assignNode->left(); |
| 3324 |
|
| 3325 |
if (!var->isKind(ParseNodeKind::Name)) { |
| 3326 |
return f.fail(var, "local variable is not a plain name"); |
| 3327 |
} |
| 3328 |
|
| 3329 |
TaggedParserAtomIndex name = var->as<NameNode>().name(); |
| 3330 |
|
| 3331 |
if (!CheckIdentifier(f.m(), var, name)) { |
| 3332 |
return false; |
| 3333 |
} |
| 3334 |
|
| 3335 |
ParseNode* initNode = assignNode->right(); |
| 3336 |
|
| 3337 |
NumLit lit; |
| 3338 |
if (!IsLiteralOrConst(f, initNode, &lit)) { |
| 3339 |
return f.failName( |
| 3340 |
var, "var '%s' initializer must be literal or const literal", name); |
| 3341 |
} |
| 3342 |
|
| 3343 |
if (!lit.valid()) { |
| 3344 |
return f.failName(var, "var '%s' initializer out of range", name); |
| 3345 |
} |
| 3346 |
|
| 3347 |
Type type = Type::canonicalize(Type::lit(lit)); |
| 3348 |
|
| 3349 |
return f.addLocal(var, name, type) && |
| 3350 |
types->append(type.canonicalToValType()) && inits->append(lit); |
| 3351 |
} |
| 3352 |
|
| 3353 |
static bool CheckVariables(FunctionValidatorShared& f, ParseNode** stmtIter) { |
| 3354 |
ParseNode* stmt = *stmtIter; |
| 3355 |
|
| 3356 |
uint32_t firstVar = f.numLocals(); |
| 3357 |
|
| 3358 |
ValTypeVector types; |
| 3359 |
Vector<NumLit> inits(f.fc()); |
| 3360 |
|
| 3361 |
for (; stmt && stmt->isKind(ParseNodeKind::VarStmt); |
| 3362 |
stmt = NextNonEmptyStatement(stmt)) { |
| 3363 |
for (ParseNode* var = VarListHead(stmt); var; var = NextNode(var)) { |
| 3364 |
if (!CheckVariable(f, var, &types, &inits)) { |
| 3365 |
return false; |
| 3366 |
} |
| 3367 |
} |
| 3368 |
} |
| 3369 |
|
| 3370 |
MOZ_ASSERT(f.encoder().empty()); |
| 3371 |
|
| 3372 |
if (!EncodeLocalEntries(f.encoder(), types)) { |
| 3373 |
return false; |
| 3374 |
} |
| 3375 |
|
| 3376 |
for (uint32_t i = 0; i < inits.length(); i++) { |
| 3377 |
NumLit lit = inits[i]; |
| 3378 |
if (lit.isZeroBits()) { |
| 3379 |
continue; |
| 3380 |
} |
| 3381 |
if (!f.writeConstExpr(lit)) { |
| 3382 |
return false; |
| 3383 |
} |
| 3384 |
if (!f.encoder().writeOp(Op::LocalSet)) { |
| 3385 |
return false; |
| 3386 |
} |
| 3387 |
if (!f.encoder().writeVarU32(firstVar + i)) { |
| 3388 |
return false; |
| 3389 |
} |
| 3390 |
} |
| 3391 |
|
| 3392 |
*stmtIter = stmt; |
| 3393 |
return true; |
| 3394 |
} |
| 3395 |
|
| 3396 |
template <typename Unit> |
| 3397 |
static bool CheckExpr(FunctionValidator<Unit>& f, ParseNode* expr, Type* type); |
| 3398 |
|
| 3399 |
template <typename Unit> |
| 3400 |
static bool CheckNumericLiteral(FunctionValidator<Unit>& f, ParseNode* num, |
| 3401 |
Type* type) { |
| 3402 |
NumLit lit = ExtractNumericLiteral(f.m(), num); |
| 3403 |
if (!lit.valid()) { |
| 3404 |
return f.fail(num, "numeric literal out of representable integer range"); |
| 3405 |
} |
| 3406 |
*type = Type::lit(lit); |
| 3407 |
return f.writeConstExpr(lit); |
| 3408 |
} |
| 3409 |
|
| 3410 |
static bool CheckVarRef(FunctionValidatorShared& f, ParseNode* varRef, |
| 3411 |
Type* type) { |
| 3412 |
TaggedParserAtomIndex name = varRef->as<NameNode>().name(); |
| 3413 |
|
| 3414 |
if (const FunctionValidatorShared::Local* local = f.lookupLocal(name)) { |
| 3415 |
if (!f.encoder().writeOp(Op::LocalGet)) { |
| 3416 |
return false; |
| 3417 |
} |
| 3418 |
if (!f.encoder().writeVarU32(local->slot)) { |
| 3419 |
return false; |
| 3420 |
} |
| 3421 |
*type = local->type; |
| 3422 |
return true; |
| 3423 |
} |
| 3424 |
|
| 3425 |
if (const ModuleValidatorShared::Global* global = f.lookupGlobal(name)) { |
| 3426 |
switch (global->which()) { |
| 3427 |
case ModuleValidatorShared::Global::ConstantLiteral: |
| 3428 |
*type = global->varOrConstType(); |
| 3429 |
return f.writeConstExpr(global->constLiteralValue()); |
| 3430 |
case ModuleValidatorShared::Global::ConstantImport: |
| 3431 |
case ModuleValidatorShared::Global::Variable: { |
| 3432 |
*type = global->varOrConstType(); |
| 3433 |
return f.encoder().writeOp(Op::GlobalGet) && |
| 3434 |
f.encoder().writeVarU32(global->varOrConstIndex()); |
| 3435 |
} |
| 3436 |
case ModuleValidatorShared::Global::Function: |
| 3437 |
case ModuleValidatorShared::Global::FFI: |
| 3438 |
case ModuleValidatorShared::Global::MathBuiltinFunction: |
| 3439 |
case ModuleValidatorShared::Global::Table: |
| 3440 |
case ModuleValidatorShared::Global::ArrayView: |
| 3441 |
case ModuleValidatorShared::Global::ArrayViewCtor: |
| 3442 |
break; |
| 3443 |
} |
| 3444 |
return f.failName(varRef, |
| 3445 |
"'%s' may not be accessed by ordinary expressions", name); |
| 3446 |
} |
| 3447 |
|
| 3448 |
return f.failName(varRef, "'%s' not found in local or asm.js module scope", |
| 3449 |
name); |
| 3450 |
} |
| 3451 |
|
| 3452 |
static inline bool IsLiteralOrConstInt(FunctionValidatorShared& f, |
| 3453 |
ParseNode* pn, uint32_t* u32) { |
| 3454 |
NumLit lit; |
| 3455 |
if (!IsLiteralOrConst(f, pn, &lit)) { |
| 3456 |
return false; |
| 3457 |
} |
| 3458 |
|
| 3459 |
return IsLiteralInt(lit, u32); |
| 3460 |
} |
| 3461 |
|
| 3462 |
static const int32_t NoMask = -1; |
| 3463 |
|
| 3464 |
template <typename Unit> |
| 3465 |
static bool CheckArrayAccess(FunctionValidator<Unit>& f, ParseNode* viewName, |
| 3466 |
ParseNode* indexExpr, Scalar::Type* viewType) { |
| 3467 |
if (!viewName->isKind(ParseNodeKind::Name)) { |
| 3468 |
return f.fail(viewName, |
| 3469 |
"base of array access must be a typed array view name"); |
| 3470 |
} |
| 3471 |
|
| 3472 |
const ModuleValidatorShared::Global* global = |
| 3473 |
f.lookupGlobal(viewName->as<NameNode>().name()); |
| 3474 |
if (!global || global->which() != ModuleValidatorShared::Global::ArrayView) { |
| 3475 |
return f.fail(viewName, |
| 3476 |
"base of array access must be a typed array view name"); |
| 3477 |
} |
| 3478 |
|
| 3479 |
*viewType = global->viewType(); |
| 3480 |
|
| 3481 |
uint32_t index; |
| 3482 |
if (IsLiteralOrConstInt(f, indexExpr, &index)) { |
| 3483 |
uint64_t byteOffset = uint64_t(index) << TypedArrayShift(*viewType); |
| 3484 |
uint64_t width = TypedArrayElemSize(*viewType); |
| 3485 |
if (!f.m().tryConstantAccess(byteOffset, width)) { |
| 3486 |
return f.fail(indexExpr, "constant index out of range"); |
| 3487 |
} |
| 3488 |
|
| 3489 |
return f.writeInt32Lit(byteOffset); |
| 3490 |
} |
| 3491 |
|
| 3492 |
// Mask off the low bits to account for the clearing effect of a right shift |
| 3493 |
// followed by the left shift implicit in the array access. E.g., H32[i>>2] |
| 3494 |
// loses the low two bits. |
| 3495 |
int32_t mask = ~(TypedArrayElemSize(*viewType) - 1); |
| 3496 |
|
| 3497 |
if (indexExpr->isKind(ParseNodeKind::RshExpr)) { |
| 3498 |
ParseNode* shiftAmountNode = BitwiseRight(indexExpr); |
| 3499 |
|
| 3500 |
uint32_t shift; |
| 3501 |
if (!IsLiteralInt(f.m(), shiftAmountNode, &shift)) { |
| 3502 |
return f.failf(shiftAmountNode, "shift amount must be constant"); |
| 3503 |
} |
| 3504 |
|
| 3505 |
unsigned requiredShift = TypedArrayShift(*viewType); |
| 3506 |
if (shift != requiredShift) { |
| 3507 |
return f.failf(shiftAmountNode, "shift amount must be %u", requiredShift); |
| 3508 |
} |
| 3509 |
|
| 3510 |
ParseNode* pointerNode = BitwiseLeft(indexExpr); |
| 3511 |
|
| 3512 |
Type pointerType; |
| 3513 |
if (!CheckExpr(f, pointerNode, &pointerType)) { |
| 3514 |
return false; |
| 3515 |
} |
| 3516 |
|
| 3517 |
if (!pointerType.isIntish()) { |
| 3518 |
return f.failf(pointerNode, "%s is not a subtype of int", |
| 3519 |
pointerType.toChars()); |
| 3520 |
} |
| 3521 |
} else { |
| 3522 |
// For legacy scalar access compatibility, accept Int8/Uint8 accesses |
| 3523 |
// with no shift. |
| 3524 |
if (TypedArrayShift(*viewType) != 0) { |
| 3525 |
return f.fail( |
| 3526 |
indexExpr, |
| 3527 |
"index expression isn't shifted; must be an Int8/Uint8 access"); |
| 3528 |
} |
| 3529 |
|
| 3530 |
MOZ_ASSERT(mask == NoMask); |
| 3531 |
|
| 3532 |
ParseNode* pointerNode = indexExpr; |
| 3533 |
|
| 3534 |
Type pointerType; |
| 3535 |
if (!CheckExpr(f, pointerNode, &pointerType)) { |
| 3536 |
return false; |
| 3537 |
} |
| 3538 |
if (!pointerType.isInt()) { |
| 3539 |
return f.failf(pointerNode, "%s is not a subtype of int", |
| 3540 |
pointerType.toChars()); |
| 3541 |
} |
| 3542 |
} |
| 3543 |
|
| 3544 |
// Don't generate the mask op if there is no need for it which could happen |
| 3545 |
// for a shift of zero. |
| 3546 |
if (mask != NoMask) { |
| 3547 |
return f.writeInt32Lit(mask) && f.encoder().writeOp(Op::I32And); |
| 3548 |
} |
| 3549 |
|
| 3550 |
return true; |
| 3551 |
} |
| 3552 |
|
| 3553 |
static bool WriteArrayAccessFlags(FunctionValidatorShared& f, |
| 3554 |
Scalar::Type viewType) { |
| 3555 |
// asm.js only has naturally-aligned accesses. |
| 3556 |
size_t align = TypedArrayElemSize(viewType); |
| 3557 |
MOZ_ASSERT(IsPowerOfTwo(align)); |
| 3558 |
if (!f.encoder().writeFixedU8(CeilingLog2(align))) { |
| 3559 |
return false; |
| 3560 |
} |
| 3561 |
|
| 3562 |
// asm.js doesn't have constant offsets, so just encode a 0. |
| 3563 |
return f.encoder().writeVarU32(0); |
| 3564 |
} |
| 3565 |
|
| 3566 |
template <typename Unit> |
| 3567 |
static bool CheckLoadArray(FunctionValidator<Unit>& f, ParseNode* elem, |
| 3568 |
Type* type) { |
| 3569 |
Scalar::Type viewType; |
| 3570 |
|
| 3571 |
if (!CheckArrayAccess(f, ElemBase(elem), ElemIndex(elem), &viewType)) { |
| 3572 |
return false; |
| 3573 |
} |
| 3574 |
|
| 3575 |
switch (viewType) { |
| 3576 |
case Scalar::Int8: |
| 3577 |
if (!f.encoder().writeOp(Op::I32Load8S)) return false; |
| 3578 |
break; |
| 3579 |
case Scalar::Uint8: |
| 3580 |
if (!f.encoder().writeOp(Op::I32Load8U)) return false; |
| 3581 |
break; |
| 3582 |
case Scalar::Int16: |
| 3583 |
if (!f.encoder().writeOp(Op::I32Load16S)) return false; |
| 3584 |
break; |
| 3585 |
case Scalar::Uint16: |
| 3586 |
if (!f.encoder().writeOp(Op::I32Load16U)) return false; |
| 3587 |
break; |
| 3588 |
case Scalar::Uint32: |
| 3589 |
case Scalar::Int32: |
| 3590 |
if (!f.encoder().writeOp(Op::I32Load)) return false; |
| 3591 |
break; |
| 3592 |
case Scalar::Float32: |
| 3593 |
if (!f.encoder().writeOp(Op::F32Load)) return false; |
| 3594 |
break; |
| 3595 |
case Scalar::Float64: |
| 3596 |
if (!f.encoder().writeOp(Op::F64Load)) return false; |
| 3597 |
break; |
| 3598 |
default: |
| 3599 |
MOZ_CRASH("unexpected scalar type"); |
| 3600 |
} |
| 3601 |
|
| 3602 |
switch (viewType) { |
| 3603 |
case Scalar::Int8: |
| 3604 |
case Scalar::Int16: |
| 3605 |
case Scalar::Int32: |
| 3606 |
case Scalar::Uint8: |
| 3607 |
case Scalar::Uint16: |
| 3608 |
case Scalar::Uint32: |
| 3609 |
*type = Type::Intish; |
| 3610 |
break; |
| 3611 |
case Scalar::Float32: |
| 3612 |
*type = Type::MaybeFloat; |
| 3613 |
break; |
| 3614 |
case Scalar::Float64: |
| 3615 |
*type = Type::MaybeDouble; |
| 3616 |
break; |
| 3617 |
default: |
| 3618 |
MOZ_CRASH("Unexpected array type"); |
| 3619 |
} |
| 3620 |
|
| 3621 |
return WriteArrayAccessFlags(f, viewType); |
| 3622 |
} |
| 3623 |
|
| 3624 |
template <typename Unit> |
| 3625 |
static bool CheckStoreArray(FunctionValidator<Unit>& f, ParseNode* lhs, |
| 3626 |
ParseNode* rhs, Type* type) { |
| 3627 |
Scalar::Type viewType; |
| 3628 |
if (!CheckArrayAccess(f, ElemBase(lhs), ElemIndex(lhs), &viewType)) { |
| 3629 |
return false; |
| 3630 |
} |
| 3631 |
|
| 3632 |
Type rhsType; |
| 3633 |
if (!CheckExpr(f, rhs, &rhsType)) { |
| 3634 |
return false; |
| 3635 |
} |
| 3636 |
|
| 3637 |
switch (viewType) { |
| 3638 |
case Scalar::Int8: |
| 3639 |
case Scalar::Int16: |
| 3640 |
case Scalar::Int32: |
| 3641 |
case Scalar::Uint8: |
| 3642 |
case Scalar::Uint16: |
| 3643 |
case Scalar::Uint32: |
| 3644 |
if (!rhsType.isIntish()) { |
| 3645 |
return f.failf(lhs, "%s is not a subtype of intish", rhsType.toChars()); |
| 3646 |
} |
| 3647 |
break; |
| 3648 |
case Scalar::Float32: |
| 3649 |
if (!rhsType.isMaybeDouble() && !rhsType.isFloatish()) { |
| 3650 |
return f.failf(lhs, "%s is not a subtype of double? or floatish", |
| 3651 |
rhsType.toChars()); |
| 3652 |
} |
| 3653 |
break; |
| 3654 |
case Scalar::Float64: |
| 3655 |
if (!rhsType.isMaybeFloat() && !rhsType.isMaybeDouble()) { |
| 3656 |
return f.failf(lhs, "%s is not a subtype of float? or double?", |
| 3657 |
rhsType.toChars()); |
| 3658 |
} |
| 3659 |
break; |
| 3660 |
default: |
| 3661 |
MOZ_CRASH("Unexpected view type"); |
| 3662 |
} |
| 3663 |
|
| 3664 |
switch (viewType) { |
| 3665 |
case Scalar::Int8: |
| 3666 |
case Scalar::Uint8: |
| 3667 |
if (!f.encoder().writeOp(MozOp::I32TeeStore8)) { |
| 3668 |
return false; |
| 3669 |
} |
| 3670 |
break; |
| 3671 |
case Scalar::Int16: |
| 3672 |
case Scalar::Uint16: |
| 3673 |
if (!f.encoder().writeOp(MozOp::I32TeeStore16)) { |
| 3674 |
return false; |
| 3675 |
} |
| 3676 |
break; |
| 3677 |
case Scalar::Int32: |
| 3678 |
case Scalar::Uint32: |
| 3679 |
if (!f.encoder().writeOp(MozOp::I32TeeStore)) { |
| 3680 |
return false; |
| 3681 |
} |
| 3682 |
break; |
| 3683 |
case Scalar::Float32: |
| 3684 |
if (rhsType.isFloatish()) { |
| 3685 |
if (!f.encoder().writeOp(MozOp::F32TeeStore)) { |
| 3686 |
return false; |
| 3687 |
} |
| 3688 |
} else { |
| 3689 |
if (!f.encoder().writeOp(MozOp::F64TeeStoreF32)) { |
| 3690 |
return false; |
| 3691 |
} |
| 3692 |
} |
| 3693 |
break; |
| 3694 |
case Scalar::Float64: |
| 3695 |
if (rhsType.isFloatish()) { |
| 3696 |
if (!f.encoder().writeOp(MozOp::F32TeeStoreF64)) { |
| 3697 |
return false; |
| 3698 |
} |
| 3699 |
} else { |
| 3700 |
if (!f.encoder().writeOp(MozOp::F64TeeStore)) { |
| 3701 |
return false; |
| 3702 |
} |
| 3703 |
} |
| 3704 |
break; |
| 3705 |
default: |
| 3706 |
MOZ_CRASH("unexpected scalar type"); |
| 3707 |
} |
| 3708 |
|
| 3709 |
if (!WriteArrayAccessFlags(f, viewType)) { |
| 3710 |
return false; |
| 3711 |
} |
| 3712 |
|
| 3713 |
*type = rhsType; |
| 3714 |
return true; |
| 3715 |
} |
| 3716 |
|
| 3717 |
template <typename Unit> |
| 3718 |
static bool CheckAssignName(FunctionValidator<Unit>& f, ParseNode* lhs, |
| 3719 |
ParseNode* rhs, Type* type) { |
| 3720 |
TaggedParserAtomIndex name = lhs->as<NameNode>().name(); |
| 3721 |
|
| 3722 |
if (const FunctionValidatorShared::Local* lhsVar = f.lookupLocal(name)) { |
| 3723 |
Type rhsType; |
| 3724 |
if (!CheckExpr(f, rhs, &rhsType)) { |
| 3725 |
return false; |
| 3726 |
} |
| 3727 |
|
| 3728 |
if (!f.encoder().writeOp(Op::LocalTee)) { |
| 3729 |
return false; |
| 3730 |
} |
| 3731 |
if (!f.encoder().writeVarU32(lhsVar->slot)) { |
| 3732 |
return false; |
| 3733 |
} |
| 3734 |
|
| 3735 |
if (!(rhsType <= lhsVar->type)) { |
| 3736 |
return f.failf(lhs, "%s is not a subtype of %s", rhsType.toChars(), |
| 3737 |
lhsVar->type.toChars()); |
| 3738 |
} |
| 3739 |
*type = rhsType; |
| 3740 |
return true; |
| 3741 |
} |
| 3742 |
|
| 3743 |
if (const ModuleValidatorShared::Global* global = f.lookupGlobal(name)) { |
| 3744 |
if (global->which() != ModuleValidatorShared::Global::Variable) { |
| 3745 |
return f.failName(lhs, "'%s' is not a mutable variable", name); |
| 3746 |
} |
| 3747 |
|
| 3748 |
Type rhsType; |
| 3749 |
if (!CheckExpr(f, rhs, &rhsType)) { |
| 3750 |
return false; |
| 3751 |
} |
| 3752 |
|
| 3753 |
Type globType = global->varOrConstType(); |
| 3754 |
if (!(rhsType <= globType)) { |
| 3755 |
return f.failf(lhs, "%s is not a subtype of %s", rhsType.toChars(), |
| 3756 |
globType.toChars()); |
| 3757 |
} |
| 3758 |
if (!f.encoder().writeOp(MozOp::TeeGlobal)) { |
| 3759 |
return false; |
| 3760 |
} |
| 3761 |
if (!f.encoder().writeVarU32(global->varOrConstIndex())) { |
| 3762 |
return false; |
| 3763 |
} |
| 3764 |
|
| 3765 |
*type = rhsType; |
| 3766 |
return true; |
| 3767 |
} |
| 3768 |
|
| 3769 |
return f.failName(lhs, "'%s' not found in local or asm.js module scope", |
| 3770 |
name); |
| 3771 |
} |
| 3772 |
|
| 3773 |
template <typename Unit> |
| 3774 |
static bool CheckAssign(FunctionValidator<Unit>& f, ParseNode* assign, |
| 3775 |
Type* type) { |
| 3776 |
MOZ_ASSERT(assign->isKind(ParseNodeKind::AssignExpr)); |
| 3777 |
|
| 3778 |
ParseNode* lhs = BinaryLeft(assign); |
| 3779 |
ParseNode* rhs = BinaryRight(assign); |
| 3780 |
|
| 3781 |
if (lhs->getKind() == ParseNodeKind::ElemExpr) { |
| 3782 |
return CheckStoreArray(f, lhs, rhs, type); |
| 3783 |
} |
| 3784 |
|
| 3785 |
if (lhs->getKind() == ParseNodeKind::Name) { |
| 3786 |
return CheckAssignName(f, lhs, rhs, type); |
| 3787 |
} |
| 3788 |
|
| 3789 |
return f.fail( |
| 3790 |
assign, |
| 3791 |
"left-hand side of assignment must be a variable or array access"); |
| 3792 |
} |
| 3793 |
|
| 3794 |
template <typename Unit> |
| 3795 |
static bool CheckMathIMul(FunctionValidator<Unit>& f, ParseNode* call, |
| 3796 |
Type* type) { |
| 3797 |
if (CallArgListLength(call) != 2) { |
| 3798 |
return f.fail(call, "Math.imul must be passed 2 arguments"); |
| 3799 |
} |
| 3800 |
|
| 3801 |
ParseNode* lhs = CallArgList(call); |
| 3802 |
ParseNode* rhs = NextNode(lhs); |
| 3803 |
|
| 3804 |
Type lhsType; |
| 3805 |
if (!CheckExpr(f, lhs, &lhsType)) { |
| 3806 |
return false; |
| 3807 |
} |
| 3808 |
|
| 3809 |
Type rhsType; |
| 3810 |
if (!CheckExpr(f, rhs, &rhsType)) { |
| 3811 |
return false; |
| 3812 |
} |
| 3813 |
|
| 3814 |
if (!lhsType.isIntish()) { |
| 3815 |
return f.failf(lhs, "%s is not a subtype of intish", lhsType.toChars()); |
| 3816 |
} |
| 3817 |
if (!rhsType.isIntish()) { |
| 3818 |
return f.failf(rhs, "%s is not a subtype of intish", rhsType.toChars()); |
| 3819 |
} |
| 3820 |
|
| 3821 |
*type = Type::Signed; |
| 3822 |
return f.encoder().writeOp(Op::I32Mul); |
| 3823 |
} |
| 3824 |
|
| 3825 |
template <typename Unit> |
| 3826 |
static bool CheckMathClz32(FunctionValidator<Unit>& f, ParseNode* call, |
| 3827 |
Type* type) { |
| 3828 |
if (CallArgListLength(call) != 1) { |
| 3829 |
return f.fail(call, "Math.clz32 must be passed 1 argument"); |
| 3830 |
} |
| 3831 |
|
| 3832 |
ParseNode* arg = CallArgList(call); |
| 3833 |
|
| 3834 |
Type argType; |
| 3835 |
if (!CheckExpr(f, arg, &argType)) { |
| 3836 |
return false; |
| 3837 |
} |
| 3838 |
|
| 3839 |
if (!argType.isIntish()) { |
| 3840 |
return f.failf(arg, "%s is not a subtype of intish", argType.toChars()); |
| 3841 |
} |
| 3842 |
|
| 3843 |
*type = Type::Fixnum; |
| 3844 |
return f.encoder().writeOp(Op::I32Clz); |
| 3845 |
} |
| 3846 |
|
| 3847 |
template <typename Unit> |
| 3848 |
static bool CheckMathAbs(FunctionValidator<Unit>& f, ParseNode* call, |
| 3849 |
Type* type) { |
| 3850 |
if (CallArgListLength(call) != 1) { |
| 3851 |
return f.fail(call, "Math.abs must be passed 1 argument"); |
| 3852 |
} |
| 3853 |
|
| 3854 |
ParseNode* arg = CallArgList(call); |
| 3855 |
|
| 3856 |
Type argType; |
| 3857 |
if (!CheckExpr(f, arg, &argType)) { |
| 3858 |
return false; |
| 3859 |
} |
| 3860 |
|
| 3861 |
if (argType.isSigned()) { |
| 3862 |
*type = Type::Unsigned; |
| 3863 |
return f.encoder().writeOp(MozOp::I32Abs); |
| 3864 |
} |
| 3865 |
|
| 3866 |
if (argType.isMaybeDouble()) { |
| 3867 |
*type = Type::Double; |
| 3868 |
return f.encoder().writeOp(Op::F64Abs); |
| 3869 |
} |
| 3870 |
|
| 3871 |
if (argType.isMaybeFloat()) { |
| 3872 |
*type = Type::Floatish; |
| 3873 |
return f.encoder().writeOp(Op::F32Abs); |
| 3874 |
} |
| 3875 |
|
| 3876 |
return f.failf(call, "%s is not a subtype of signed, float? or double?", |
| 3877 |
argType.toChars()); |
| 3878 |
} |
| 3879 |
|
| 3880 |
template <typename Unit> |
| 3881 |
static bool CheckMathSqrt(FunctionValidator<Unit>& f, ParseNode* call, |
| 3882 |
Type* type) { |
| 3883 |
if (CallArgListLength(call) != 1) { |
| 3884 |
return f.fail(call, "Math.sqrt must be passed 1 argument"); |
| 3885 |
} |
| 3886 |
|
| 3887 |
ParseNode* arg = CallArgList(call); |
| 3888 |
|
| 3889 |
Type argType; |
| 3890 |
if (!CheckExpr(f, arg, &argType)) { |
| 3891 |
return false; |
| 3892 |
} |
| 3893 |
|
| 3894 |
if (argType.isMaybeDouble()) { |
| 3895 |
*type = Type::Double; |
| 3896 |
return f.encoder().writeOp(Op::F64Sqrt); |
| 3897 |
} |
| 3898 |
|
| 3899 |
if (argType.isMaybeFloat()) { |
| 3900 |
*type = Type::Floatish; |
| 3901 |
return f.encoder().writeOp(Op::F32Sqrt); |
| 3902 |
} |
| 3903 |
|
| 3904 |
return f.failf(call, "%s is neither a subtype of double? nor float?", |
| 3905 |
argType.toChars()); |
| 3906 |
} |
| 3907 |
|
| 3908 |
template <typename Unit> |
| 3909 |
static bool CheckMathMinMax(FunctionValidator<Unit>& f, ParseNode* callNode, |
| 3910 |
bool isMax, Type* type) { |
| 3911 |
if (CallArgListLength(callNode) < 2) { |
| 3912 |
return f.fail(callNode, "Math.min/max must be passed at least 2 arguments"); |
| 3913 |
} |
| 3914 |
|
| 3915 |
ParseNode* firstArg = CallArgList(callNode); |
| 3916 |
Type firstType; |
| 3917 |
if (!CheckExpr(f, firstArg, &firstType)) { |
| 3918 |
return false; |
| 3919 |
} |
| 3920 |
|
| 3921 |
Op op = Op::Limit; |
| 3922 |
MozOp mozOp = MozOp::Limit; |
| 3923 |
if (firstType.isMaybeDouble()) { |
| 3924 |
*type = Type::Double; |
| 3925 |
firstType = Type::MaybeDouble; |
| 3926 |
op = isMax ? Op::F64Max : Op::F64Min; |
| 3927 |
} else if (firstType.isMaybeFloat()) { |
| 3928 |
*type = Type::Float; |
| 3929 |
firstType = Type::MaybeFloat; |
| 3930 |
op = isMax ? Op::F32Max : Op::F32Min; |
| 3931 |
} else if (firstType.isSigned()) { |
| 3932 |
*type = Type::Signed; |
| 3933 |
firstType = Type::Signed; |
| 3934 |
mozOp = isMax ? MozOp::I32Max : MozOp::I32Min; |
| 3935 |
} else { |
| 3936 |
return f.failf(firstArg, "%s is not a subtype of double?, float? or signed", |
| 3937 |
firstType.toChars()); |
| 3938 |
} |
| 3939 |
|
| 3940 |
unsigned numArgs = CallArgListLength(callNode); |
| 3941 |
ParseNode* nextArg = NextNode(firstArg); |
| 3942 |
for (unsigned i = 1; i < numArgs; i++, nextArg = NextNode(nextArg)) { |
| 3943 |
Type nextType; |
| 3944 |
if (!CheckExpr(f, nextArg, &nextType)) { |
| 3945 |
return false; |
| 3946 |
} |
| 3947 |
if (!(nextType <= firstType)) { |
| 3948 |
return f.failf(nextArg, "%s is not a subtype of %s", nextType.toChars(), |
| 3949 |
firstType.toChars()); |
| 3950 |
} |
| 3951 |
|
| 3952 |
if (op != Op::Limit) { |
| 3953 |
if (!f.encoder().writeOp(op)) { |
| 3954 |
return false; |
| 3955 |
} |
| 3956 |
} else { |
| 3957 |
if (!f.encoder().writeOp(mozOp)) { |
| 3958 |
return false; |
| 3959 |
} |
| 3960 |
} |
| 3961 |
} |
| 3962 |
|
| 3963 |
return true; |
| 3964 |
} |
| 3965 |
|
| 3966 |
using CheckArgType = bool (*)(FunctionValidatorShared& f, ParseNode* argNode, |
| 3967 |
Type type); |
| 3968 |
|
| 3969 |
template <CheckArgType checkArg, typename Unit> |
| 3970 |
static bool CheckCallArgs(FunctionValidator<Unit>& f, ParseNode* callNode, |
| 3971 |
ValTypeVector* args) { |
| 3972 |
ParseNode* argNode = CallArgList(callNode); |
| 3973 |
for (unsigned i = 0; i < CallArgListLength(callNode); |
| 3974 |
i++, argNode = NextNode(argNode)) { |
| 3975 |
Type type; |
| 3976 |
if (!CheckExpr(f, argNode, &type)) { |
| 3977 |
return false; |
| 3978 |
} |
| 3979 |
|
| 3980 |
if (!checkArg(f, argNode, type)) { |
| 3981 |
return false; |
| 3982 |
} |
| 3983 |
|
| 3984 |
if (!args->append(Type::canonicalize(type).canonicalToValType())) { |
| 3985 |
return false; |
| 3986 |
} |
| 3987 |
} |
| 3988 |
if (args->length() > MaxParams) { |
| 3989 |
return f.fail(callNode, "too many parameters"); |
| 3990 |
} |
| 3991 |
return true; |
| 3992 |
} |
| 3993 |
|
| 3994 |
static bool CheckSignatureAgainstExisting(ModuleValidatorShared& m, |
| 3995 |
ParseNode* usepn, const FuncType& sig, |
| 3996 |
const FuncType& existing) { |
| 3997 |
if (!FuncType::strictlyEquals(sig, existing)) { |
| 3998 |
return m.failf(usepn, "incompatible argument types to function"); |
| 3999 |
} |
| 4000 |
return true; |
| 4001 |
} |
| 4002 |
|
| 4003 |
template <typename Unit> |
| 4004 |
static bool CheckFunctionSignature(ModuleValidator<Unit>& m, ParseNode* usepn, |
| 4005 |
FuncType&& sig, TaggedParserAtomIndex name, |
| 4006 |
ModuleValidatorShared::Func** func) { |
| 4007 |
ModuleValidatorShared::Func* existing = m.lookupFuncDef(name); |
| 4008 |
if (!existing) { |
| 4009 |
if (!CheckModuleLevelName(m, usepn, name)) { |
| 4010 |
return false; |
| 4011 |
} |
| 4012 |
return m.addFuncDef(name, usepn->pn_pos.begin, std::move(sig), func); |
| 4013 |
} |
| 4014 |
|
| 4015 |
const FuncType& existingSig = |
| 4016 |
m.codeMeta()->types->type(existing->sigIndex()).funcType(); |
| 4017 |
|
| 4018 |
if (!CheckSignatureAgainstExisting(m, usepn, sig, existingSig)) { |
| 4019 |
return false; |
| 4020 |
} |
| 4021 |
|
| 4022 |
*func = existing; |
| 4023 |
return true; |
| 4024 |
} |
| 4025 |
|
| 4026 |
static bool CheckIsArgType(FunctionValidatorShared& f, ParseNode* argNode, |
| 4027 |
Type type) { |
| 4028 |
if (!type.isArgType()) { |
| 4029 |
return f.failf(argNode, "%s is not a subtype of int, float, or double", |
| 4030 |
type.toChars()); |
| 4031 |
} |
| 4032 |
return true; |
| 4033 |
} |
| 4034 |
|
| 4035 |
template <typename Unit> |
| 4036 |
static bool CheckInternalCall(FunctionValidator<Unit>& f, ParseNode* callNode, |
| 4037 |
TaggedParserAtomIndex calleeName, Type ret, |
| 4038 |
Type* type) { |
| 4039 |
MOZ_ASSERT(ret.isCanonical()); |
| 4040 |
|
| 4041 |
ValTypeVector args; |
| 4042 |
if (!CheckCallArgs<CheckIsArgType>(f, callNode, &args)) { |
| 4043 |
return false; |
| 4044 |
} |
| 4045 |
|
| 4046 |
ValTypeVector results; |
| 4047 |
Maybe<ValType> retType = ret.canonicalToReturnType(); |
| 4048 |
if (retType && !results.append(retType.ref())) { |
| 4049 |
return false; |
| 4050 |
} |
| 4051 |
|
| 4052 |
FuncType sig(std::move(args), std::move(results)); |
| 4053 |
|
| 4054 |
ModuleValidatorShared::Func* callee; |
| 4055 |
if (!CheckFunctionSignature(f.m(), callNode, std::move(sig), calleeName, |
| 4056 |
&callee)) { |
| 4057 |
return false; |
| 4058 |
} |
| 4059 |
|
| 4060 |
if (!f.writeCall(callNode, MozOp::OldCallDirect)) { |
| 4061 |
return false; |
| 4062 |
} |
| 4063 |
|
| 4064 |
if (!f.encoder().writeVarU32(callee->funcDefIndex())) { |
| 4065 |
return false; |
| 4066 |
} |
| 4067 |
|
| 4068 |
*type = Type::ret(ret); |
| 4069 |
return true; |
| 4070 |
} |
| 4071 |
|
| 4072 |
template <typename Unit> |
| 4073 |
static bool CheckFuncPtrTableAgainstExisting(ModuleValidator<Unit>& m, |
| 4074 |
ParseNode* usepn, |
| 4075 |
TaggedParserAtomIndex name, |
| 4076 |
FuncType&& sig, unsigned mask, |
| 4077 |
uint32_t* tableIndex) { |
| 4078 |
if (const ModuleValidatorShared::Global* existing = m.lookupGlobal(name)) { |
| 4079 |
if (existing->which() != ModuleValidatorShared::Global::Table) { |
| 4080 |
return m.failName(usepn, "'%s' is not a function-pointer table", name); |
| 4081 |
} |
| 4082 |
|
| 4083 |
ModuleValidatorShared::Table& table = m.table(existing->tableIndex()); |
| 4084 |
if (mask != table.mask()) { |
| 4085 |
return m.failf(usepn, "mask does not match previous value (%u)", |
| 4086 |
table.mask()); |
| 4087 |
} |
| 4088 |
|
| 4089 |
if (!CheckSignatureAgainstExisting( |
| 4090 |
m, usepn, sig, |
| 4091 |
m.codeMeta()->types->type(table.sigIndex()).funcType())) { |
| 4092 |
return false; |
| 4093 |
} |
| 4094 |
|
| 4095 |
*tableIndex = existing->tableIndex(); |
| 4096 |
return true; |
| 4097 |
} |
| 4098 |
|
| 4099 |
if (!CheckModuleLevelName(m, usepn, name)) { |
| 4100 |
return false; |
| 4101 |
} |
| 4102 |
|
| 4103 |
return m.declareFuncPtrTable(std::move(sig), name, usepn->pn_pos.begin, mask, |
| 4104 |
tableIndex); |
| 4105 |
} |
| 4106 |
|
| 4107 |
template <typename Unit> |
| 4108 |
static bool CheckFuncPtrCall(FunctionValidator<Unit>& f, ParseNode* callNode, |
| 4109 |
Type ret, Type* type) { |
| 4110 |
MOZ_ASSERT(ret.isCanonical()); |
| 4111 |
|
| 4112 |
ParseNode* callee = CallCallee(callNode); |
| 4113 |
ParseNode* tableNode = ElemBase(callee); |
| 4114 |
ParseNode* indexExpr = ElemIndex(callee); |
| 4115 |
|
| 4116 |
if (!tableNode->isKind(ParseNodeKind::Name)) { |
| 4117 |
return f.fail(tableNode, "expecting name of function-pointer array"); |
| 4118 |
} |
| 4119 |
|
| 4120 |
TaggedParserAtomIndex name = tableNode->as<NameNode>().name(); |
| 4121 |
if (const ModuleValidatorShared::Global* existing = f.lookupGlobal(name)) { |
| 4122 |
if (existing->which() != ModuleValidatorShared::Global::Table) { |
| 4123 |
return f.failName( |
| 4124 |
tableNode, "'%s' is not the name of a function-pointer array", name); |
| 4125 |
} |
| 4126 |
} |
| 4127 |
|
| 4128 |
if (!indexExpr->isKind(ParseNodeKind::BitAndExpr)) { |
| 4129 |
return f.fail(indexExpr, |
| 4130 |
"function-pointer table index expression needs & mask"); |
| 4131 |
} |
| 4132 |
|
| 4133 |
ParseNode* indexNode = BitwiseLeft(indexExpr); |
| 4134 |
ParseNode* maskNode = BitwiseRight(indexExpr); |
| 4135 |
|
| 4136 |
uint32_t mask; |
| 4137 |
if (!IsLiteralInt(f.m(), maskNode, &mask) || mask == UINT32_MAX || |
| 4138 |
!IsPowerOfTwo(mask + 1)) { |
| 4139 |
return f.fail(maskNode, |
| 4140 |
"function-pointer table index mask value must be a power of " |
| 4141 |
"two minus 1"); |
| 4142 |
} |
| 4143 |
|
| 4144 |
Type indexType; |
| 4145 |
if (!CheckExpr(f, indexNode, &indexType)) { |
| 4146 |
return false; |
| 4147 |
} |
| 4148 |
|
| 4149 |
if (!indexType.isIntish()) { |
| 4150 |
return f.failf(indexNode, "%s is not a subtype of intish", |
| 4151 |
indexType.toChars()); |
| 4152 |
} |
| 4153 |
|
| 4154 |
ValTypeVector args; |
| 4155 |
if (!CheckCallArgs<CheckIsArgType>(f, callNode, &args)) { |
| 4156 |
return false; |
| 4157 |
} |
| 4158 |
|
| 4159 |
ValTypeVector results; |
| 4160 |
Maybe<ValType> retType = ret.canonicalToReturnType(); |
| 4161 |
if (retType && !results.append(retType.ref())) { |
| 4162 |
return false; |
| 4163 |
} |
| 4164 |
|
| 4165 |
FuncType sig(std::move(args), std::move(results)); |
| 4166 |
|
| 4167 |
uint32_t tableIndex; |
| 4168 |
if (!CheckFuncPtrTableAgainstExisting(f.m(), tableNode, name, std::move(sig), |
| 4169 |
mask, &tableIndex)) { |
| 4170 |
return false; |
| 4171 |
} |
| 4172 |
|
| 4173 |
if (!f.writeCall(callNode, MozOp::OldCallIndirect)) { |
| 4174 |
return false; |
| 4175 |
} |
| 4176 |
|
| 4177 |
// Call signature |
| 4178 |
if (!f.encoder().writeVarU32(f.m().table(tableIndex).sigIndex())) { |
| 4179 |
return false; |
| 4180 |
} |
| 4181 |
|
| 4182 |
*type = Type::ret(ret); |
| 4183 |
return true; |
| 4184 |
} |
| 4185 |
|
| 4186 |
static bool CheckIsExternType(FunctionValidatorShared& f, ParseNode* argNode, |
| 4187 |
Type type) { |
| 4188 |
if (!type.isExtern()) { |
| 4189 |
return f.failf(argNode, "%s is not a subtype of extern", type.toChars()); |
| 4190 |
} |
| 4191 |
return true; |
| 4192 |
} |
| 4193 |
|
| 4194 |
template <typename Unit> |
| 4195 |
static bool CheckFFICall(FunctionValidator<Unit>& f, ParseNode* callNode, |
| 4196 |
unsigned ffiIndex, Type ret, Type* type) { |
| 4197 |
MOZ_ASSERT(ret.isCanonical()); |
| 4198 |
|
| 4199 |
TaggedParserAtomIndex calleeName = |
| 4200 |
CallCallee(callNode)->as<NameNode>().name(); |
| 4201 |
|
| 4202 |
if (ret.isFloat()) { |
| 4203 |
return f.fail(callNode, "FFI calls can't return float"); |
| 4204 |
} |
| 4205 |
|
| 4206 |
ValTypeVector args; |
| 4207 |
if (!CheckCallArgs<CheckIsExternType>(f, callNode, &args)) { |
| 4208 |
return false; |
| 4209 |
} |
| 4210 |
|
| 4211 |
ValTypeVector results; |
| 4212 |
Maybe<ValType> retType = ret.canonicalToReturnType(); |
| 4213 |
if (retType && !results.append(retType.ref())) { |
| 4214 |
return false; |
| 4215 |
} |
| 4216 |
|
| 4217 |
FuncType sig(std::move(args), std::move(results)); |
| 4218 |
|
| 4219 |
uint32_t importIndex; |
| 4220 |
if (!f.m().declareImport(calleeName, std::move(sig), ffiIndex, |
| 4221 |
&importIndex)) { |
| 4222 |
return false; |
| 4223 |
} |
| 4224 |
|
| 4225 |
if (!f.writeCall(callNode, Op::Call)) { |
| 4226 |
return false; |
| 4227 |
} |
| 4228 |
|
| 4229 |
if (!f.encoder().writeVarU32(importIndex)) { |
| 4230 |
return false; |
| 4231 |
} |
| 4232 |
|
| 4233 |
*type = Type::ret(ret); |
| 4234 |
return true; |
| 4235 |
} |
| 4236 |
|
| 4237 |
static bool CheckFloatCoercionArg(FunctionValidatorShared& f, |
| 4238 |
ParseNode* inputNode, Type inputType) { |
| 4239 |
if (inputType.isMaybeDouble()) { |
| 4240 |
return f.encoder().writeOp(Op::F32DemoteF64); |
| 4241 |
} |
| 4242 |
if (inputType.isSigned()) { |
| 4243 |
return f.encoder().writeOp(Op::F32ConvertI32S); |
| 4244 |
} |
| 4245 |
if (inputType.isUnsigned()) { |
| 4246 |
return f.encoder().writeOp(Op::F32ConvertI32U); |
| 4247 |
} |
| 4248 |
if (inputType.isFloatish()) { |
| 4249 |
return true; |
| 4250 |
} |
| 4251 |
|
| 4252 |
return f.failf(inputNode, |
| 4253 |
"%s is not a subtype of signed, unsigned, double? or floatish", |
| 4254 |
inputType.toChars()); |
| 4255 |
} |
| 4256 |
|
| 4257 |
template <typename Unit> |
| 4258 |
static bool CheckCoercedCall(FunctionValidator<Unit>& f, ParseNode* call, |
| 4259 |
Type ret, Type* type); |
| 4260 |
|
| 4261 |
template <typename Unit> |
| 4262 |
static bool CheckCoercionArg(FunctionValidator<Unit>& f, ParseNode* arg, |
| 4263 |
Type expected, Type* type) { |
| 4264 |
MOZ_ASSERT(expected.isCanonicalValType()); |
| 4265 |
|
| 4266 |
if (arg->isKind(ParseNodeKind::CallExpr)) { |
| 4267 |
return CheckCoercedCall(f, arg, expected, type); |
| 4268 |
} |
| 4269 |
|
| 4270 |
Type argType; |
| 4271 |
if (!CheckExpr(f, arg, &argType)) { |
| 4272 |
return false; |
| 4273 |
} |
| 4274 |
|
| 4275 |
if (expected.isFloat()) { |
| 4276 |
if (!CheckFloatCoercionArg(f, arg, argType)) { |
| 4277 |
return false; |
| 4278 |
} |
| 4279 |
} else { |
| 4280 |
MOZ_CRASH("not call coercions"); |
| 4281 |
} |
| 4282 |
|
| 4283 |
*type = Type::ret(expected); |
| 4284 |
return true; |
| 4285 |
} |
| 4286 |
|
| 4287 |
template <typename Unit> |
| 4288 |
static bool CheckMathFRound(FunctionValidator<Unit>& f, ParseNode* callNode, |
| 4289 |
Type* type) { |
| 4290 |
if (CallArgListLength(callNode) != 1) { |
| 4291 |
return f.fail(callNode, "Math.fround must be passed 1 argument"); |
| 4292 |
} |
| 4293 |
|
| 4294 |
ParseNode* argNode = CallArgList(callNode); |
| 4295 |
Type argType; |
| 4296 |
if (!CheckCoercionArg(f, argNode, Type::Float, &argType)) { |
| 4297 |
return false; |
| 4298 |
} |
| 4299 |
|
| 4300 |
MOZ_ASSERT(argType == Type::Float); |
| 4301 |
*type = Type::Float; |
| 4302 |
return true; |
| 4303 |
} |
| 4304 |
|
| 4305 |
template <typename Unit> |
| 4306 |
static bool CheckMathBuiltinCall(FunctionValidator<Unit>& f, |
| 4307 |
ParseNode* callNode, |
| 4308 |
AsmJSMathBuiltinFunction func, Type* type) { |
| 4309 |
unsigned arity = 0; |
| 4310 |
Op f32 = Op::Limit; |
| 4311 |
Op f64 = Op::Limit; |
| 4312 |
MozOp mozf64 = MozOp::Limit; |
| 4313 |
switch (func) { |
| 4314 |
case AsmJSMathBuiltin_imul: |
| 4315 |
return CheckMathIMul(f, callNode, type); |
| 4316 |
case AsmJSMathBuiltin_clz32: |
| 4317 |
return CheckMathClz32(f, callNode, type); |
| 4318 |
case AsmJSMathBuiltin_abs: |
| 4319 |
return CheckMathAbs(f, callNode, type); |
| 4320 |
case AsmJSMathBuiltin_sqrt: |
| 4321 |
return CheckMathSqrt(f, callNode, type); |
| 4322 |
case AsmJSMathBuiltin_fround: |
| 4323 |
return CheckMathFRound(f, callNode, type); |
| 4324 |
case AsmJSMathBuiltin_min: |
| 4325 |
return CheckMathMinMax(f, callNode, /* isMax = */ false, type); |
| 4326 |
case AsmJSMathBuiltin_max: |
| 4327 |
return CheckMathMinMax(f, callNode, /* isMax = */ true, type); |
| 4328 |
case AsmJSMathBuiltin_ceil: |
| 4329 |
arity = 1; |
| 4330 |
f64 = Op::F64Ceil; |
| 4331 |
f32 = Op::F32Ceil; |
| 4332 |
break; |
| 4333 |
case AsmJSMathBuiltin_floor: |
| 4334 |
arity = 1; |
| 4335 |
f64 = Op::F64Floor; |
| 4336 |
f32 = Op::F32Floor; |
| 4337 |
break; |
| 4338 |
case AsmJSMathBuiltin_sin: |
| 4339 |
arity = 1; |
| 4340 |
if (!f.m().alwaysUseFdlibm()) { |
| 4341 |
mozf64 = MozOp::F64SinNative; |
| 4342 |
} else { |
| 4343 |
mozf64 = MozOp::F64SinFdlibm; |
| 4344 |
} |
| 4345 |
f32 = Op::Unreachable; |
| 4346 |
break; |
| 4347 |
case AsmJSMathBuiltin_cos: |
| 4348 |
arity = 1; |
| 4349 |
if (!f.m().alwaysUseFdlibm()) { |
| 4350 |
mozf64 = MozOp::F64CosNative; |
| 4351 |
} else { |
| 4352 |
mozf64 = MozOp::F64CosFdlibm; |
| 4353 |
} |
| 4354 |
f32 = Op::Unreachable; |
| 4355 |
break; |
| 4356 |
case AsmJSMathBuiltin_tan: |
| 4357 |
arity = 1; |
| 4358 |
if (!f.m().alwaysUseFdlibm()) { |
| 4359 |
mozf64 = MozOp::F64TanNative; |
| 4360 |
} else { |
| 4361 |
mozf64 = MozOp::F64TanFdlibm; |
| 4362 |
} |
| 4363 |
f32 = Op::Unreachable; |
| 4364 |
break; |
| 4365 |
case AsmJSMathBuiltin_asin: |
| 4366 |
arity = 1; |
| 4367 |
mozf64 = MozOp::F64Asin; |
| 4368 |
f32 = Op::Unreachable; |
| 4369 |
break; |
| 4370 |
case AsmJSMathBuiltin_acos: |
| 4371 |
arity = 1; |
| 4372 |
mozf64 = MozOp::F64Acos; |
| 4373 |
f32 = Op::Unreachable; |
| 4374 |
break; |
| 4375 |
case AsmJSMathBuiltin_atan: |
| 4376 |
arity = 1; |
| 4377 |
mozf64 = MozOp::F64Atan; |
| 4378 |
f32 = Op::Unreachable; |
| 4379 |
break; |
| 4380 |
case AsmJSMathBuiltin_exp: |
| 4381 |
arity = 1; |
| 4382 |
mozf64 = MozOp::F64Exp; |
| 4383 |
f32 = Op::Unreachable; |
| 4384 |
break; |
| 4385 |
case AsmJSMathBuiltin_log: |
| 4386 |
arity = 1; |
| 4387 |
mozf64 = MozOp::F64Log; |
| 4388 |
f32 = Op::Unreachable; |
| 4389 |
break; |
| 4390 |
case AsmJSMathBuiltin_pow: |
| 4391 |
arity = 2; |
| 4392 |
mozf64 = MozOp::F64Pow; |
| 4393 |
f32 = Op::Unreachable; |
| 4394 |
break; |
| 4395 |
case AsmJSMathBuiltin_atan2: |
| 4396 |
arity = 2; |
| 4397 |
mozf64 = MozOp::F64Atan2; |
| 4398 |
f32 = Op::Unreachable; |
| 4399 |
break; |
| 4400 |
default: |
| 4401 |
MOZ_CRASH("unexpected mathBuiltin function"); |
| 4402 |
} |
| 4403 |
|
| 4404 |
unsigned actualArity = CallArgListLength(callNode); |
| 4405 |
if (actualArity != arity) { |
| 4406 |
return f.failf(callNode, "call passed %u arguments, expected %u", |
| 4407 |
actualArity, arity); |
| 4408 |
} |
| 4409 |
|
| 4410 |
if (!f.prepareCall(callNode)) { |
| 4411 |
return false; |
| 4412 |
} |
| 4413 |
|
| 4414 |
Type firstType; |
| 4415 |
ParseNode* argNode = CallArgList(callNode); |
| 4416 |
if (!CheckExpr(f, argNode, &firstType)) { |
| 4417 |
return false; |
| 4418 |
} |
| 4419 |
|
| 4420 |
if (!firstType.isMaybeFloat() && !firstType.isMaybeDouble()) { |
| 4421 |
return f.fail( |
| 4422 |
argNode, |
| 4423 |
"arguments to math call should be a subtype of double? or float?"); |
| 4424 |
} |
| 4425 |
|
| 4426 |
bool opIsDouble = firstType.isMaybeDouble(); |
| 4427 |
if (!opIsDouble && f32 == Op::Unreachable) { |
| 4428 |
return f.fail(callNode, "math builtin cannot be used as float"); |
| 4429 |
} |
| 4430 |
|
| 4431 |
if (arity == 2) { |
| 4432 |
Type secondType; |
| 4433 |
argNode = NextNode(argNode); |
| 4434 |
if (!CheckExpr(f, argNode, &secondType)) { |
| 4435 |
return false; |
| 4436 |
} |
| 4437 |
|
| 4438 |
if (firstType.isMaybeDouble() && !secondType.isMaybeDouble()) { |
| 4439 |
return f.fail( |
| 4440 |
argNode, |
| 4441 |
"both arguments to math builtin call should be the same type"); |
| 4442 |
} |
| 4443 |
if (firstType.isMaybeFloat() && !secondType.isMaybeFloat()) { |
| 4444 |
return f.fail( |
| 4445 |
argNode, |
| 4446 |
"both arguments to math builtin call should be the same type"); |
| 4447 |
} |
| 4448 |
} |
| 4449 |
|
| 4450 |
if (opIsDouble) { |
| 4451 |
if (f64 != Op::Limit) { |
| 4452 |
if (!f.encoder().writeOp(f64)) { |
| 4453 |
return false; |
| 4454 |
} |
| 4455 |
} else { |
| 4456 |
if (!f.encoder().writeOp(mozf64)) { |
| 4457 |
return false; |
| 4458 |
} |
| 4459 |
} |
| 4460 |
} else { |
| 4461 |
if (!f.encoder().writeOp(f32)) { |
| 4462 |
return false; |
| 4463 |
} |
| 4464 |
} |
| 4465 |
|
| 4466 |
*type = opIsDouble ? Type::Double : Type::Floatish; |
| 4467 |
return true; |
| 4468 |
} |
| 4469 |
|
| 4470 |
template <typename Unit> |
| 4471 |
static bool CheckUncoercedCall(FunctionValidator<Unit>& f, ParseNode* expr, |
| 4472 |
Type* type) { |
| 4473 |
MOZ_ASSERT(expr->isKind(ParseNodeKind::CallExpr)); |
| 4474 |
|
| 4475 |
const ModuleValidatorShared::Global* global; |
| 4476 |
if (IsCallToGlobal(f.m(), expr, &global) && global->isMathFunction()) { |
| 4477 |
return CheckMathBuiltinCall(f, expr, global->mathBuiltinFunction(), type); |
| 4478 |
} |
| 4479 |
|
| 4480 |
return f.fail( |
| 4481 |
expr, |
| 4482 |
"all function calls must be calls to standard lib math functions," |
| 4483 |
" ignored (via f(); or comma-expression), coerced to signed (via f()|0)," |
| 4484 |
" coerced to float (via fround(f())), or coerced to double (via +f())"); |
| 4485 |
} |
| 4486 |
|
| 4487 |
static bool CoerceResult(FunctionValidatorShared& f, ParseNode* expr, |
| 4488 |
Type expected, Type actual, Type* type) { |
| 4489 |
MOZ_ASSERT(expected.isCanonical()); |
| 4490 |
|
| 4491 |
// At this point, the bytecode resembles this: |
| 4492 |
// | the thing we wanted to coerce | current position |> |
| 4493 |
switch (expected.which()) { |
| 4494 |
case Type::Void: |
| 4495 |
if (!actual.isVoid()) { |
| 4496 |
if (!f.encoder().writeOp(Op::Drop)) { |
| 4497 |
return false; |
| 4498 |
} |
| 4499 |
} |
| 4500 |
break; |
| 4501 |
case Type::Int: |
| 4502 |
if (!actual.isIntish()) { |
| 4503 |
return f.failf(expr, "%s is not a subtype of intish", actual.toChars()); |
| 4504 |
} |
| 4505 |
break; |
| 4506 |
case Type::Float: |
| 4507 |
if (!CheckFloatCoercionArg(f, expr, actual)) { |
| 4508 |
return false; |
| 4509 |
} |
| 4510 |
break; |
| 4511 |
case Type::Double: |
| 4512 |
if (actual.isMaybeDouble()) { |
| 4513 |
// No conversion necessary. |
| 4514 |
} else if (actual.isMaybeFloat()) { |
| 4515 |
if (!f.encoder().writeOp(Op::F64PromoteF32)) { |
| 4516 |
return false; |
| 4517 |
} |
| 4518 |
} else if (actual.isSigned()) { |
| 4519 |
if (!f.encoder().writeOp(Op::F64ConvertI32S)) { |
| 4520 |
return false; |
| 4521 |
} |
| 4522 |
} else if (actual.isUnsigned()) { |
| 4523 |
if (!f.encoder().writeOp(Op::F64ConvertI32U)) { |
| 4524 |
return false; |
| 4525 |
} |
| 4526 |
} else { |
| 4527 |
return f.failf( |
| 4528 |
expr, "%s is not a subtype of double?, float?, signed or unsigned", |
| 4529 |
actual.toChars()); |
| 4530 |
} |
| 4531 |
break; |
| 4532 |
default: |
| 4533 |
MOZ_CRASH("unexpected uncoerced result type"); |
| 4534 |
} |
| 4535 |
|
| 4536 |
*type = Type::ret(expected); |
| 4537 |
return true; |
| 4538 |
} |
| 4539 |
|
| 4540 |
template <typename Unit> |
| 4541 |
static bool CheckCoercedMathBuiltinCall(FunctionValidator<Unit>& f, |
| 4542 |
ParseNode* callNode, |
| 4543 |
AsmJSMathBuiltinFunction func, Type ret, |
| 4544 |
Type* type) { |
| 4545 |
Type actual; |
| 4546 |
if (!CheckMathBuiltinCall(f, callNode, func, &actual)) { |
| 4547 |
return false; |
| 4548 |
} |
| 4549 |
return CoerceResult(f, callNode, ret, actual, type); |
| 4550 |
} |
| 4551 |
|
| 4552 |
template <typename Unit> |
| 4553 |
static bool CheckCoercedCall(FunctionValidator<Unit>& f, ParseNode* call, |
| 4554 |
Type ret, Type* type) { |
| 4555 |
MOZ_ASSERT(ret.isCanonical()); |
| 4556 |
|
| 4557 |
AutoCheckRecursionLimit recursion(f.fc()); |
| 4558 |
if (!recursion.checkDontReport(f.fc())) { |
| 4559 |
return f.m().failOverRecursed(); |
| 4560 |
} |
| 4561 |
|
| 4562 |
if (IsNumericLiteral(f.m(), call)) { |
| 4563 |
NumLit lit = ExtractNumericLiteral(f.m(), call); |
| 4564 |
if (!f.writeConstExpr(lit)) { |
| 4565 |
return false; |
| 4566 |
} |
| 4567 |
return CoerceResult(f, call, ret, Type::lit(lit), type); |
| 4568 |
} |
| 4569 |
|
| 4570 |
ParseNode* callee = CallCallee(call); |
| 4571 |
|
| 4572 |
if (callee->isKind(ParseNodeKind::ElemExpr)) { |
| 4573 |
return CheckFuncPtrCall(f, call, ret, type); |
| 4574 |
} |
| 4575 |
|
| 4576 |
if (!callee->isKind(ParseNodeKind::Name)) { |
| 4577 |
return f.fail(callee, "unexpected callee expression type"); |
| 4578 |
} |
| 4579 |
|
| 4580 |
TaggedParserAtomIndex calleeName = callee->as<NameNode>().name(); |
| 4581 |
|
| 4582 |
if (const ModuleValidatorShared::Global* global = |
| 4583 |
f.lookupGlobal(calleeName)) { |
| 4584 |
switch (global->which()) { |
| 4585 |
case ModuleValidatorShared::Global::FFI: |
| 4586 |
return CheckFFICall(f, call, global->ffiIndex(), ret, type); |
| 4587 |
case ModuleValidatorShared::Global::MathBuiltinFunction: |
| 4588 |
return CheckCoercedMathBuiltinCall( |
| 4589 |
f, call, global->mathBuiltinFunction(), ret, type); |
| 4590 |
case ModuleValidatorShared::Global::ConstantLiteral: |
| 4591 |
case ModuleValidatorShared::Global::ConstantImport: |
| 4592 |
case ModuleValidatorShared::Global::Variable: |
| 4593 |
case ModuleValidatorShared::Global::Table: |
| 4594 |
case ModuleValidatorShared::Global::ArrayView: |
| 4595 |
case ModuleValidatorShared::Global::ArrayViewCtor: |
| 4596 |
return f.failName(callee, "'%s' is not callable function", calleeName); |
| 4597 |
case ModuleValidatorShared::Global::Function: |
| 4598 |
break; |
| 4599 |
} |
| 4600 |
} |
| 4601 |
|
| 4602 |
return CheckInternalCall(f, call, calleeName, ret, type); |
| 4603 |
} |
| 4604 |
|
| 4605 |
template <typename Unit> |
| 4606 |
static bool CheckPos(FunctionValidator<Unit>& f, ParseNode* pos, Type* type) { |
| 4607 |
MOZ_ASSERT(pos->isKind(ParseNodeKind::PosExpr)); |
| 4608 |
ParseNode* operand = UnaryKid(pos); |
| 4609 |
|
| 4610 |
if (operand->isKind(ParseNodeKind::CallExpr)) { |
| 4611 |
return CheckCoercedCall(f, operand, Type::Double, type); |
| 4612 |
} |
| 4613 |
|
| 4614 |
Type actual; |
| 4615 |
if (!CheckExpr(f, operand, &actual)) { |
| 4616 |
return false; |
| 4617 |
} |
| 4618 |
|
| 4619 |
return CoerceResult(f, operand, Type::Double, actual, type); |
| 4620 |
} |
| 4621 |
|
| 4622 |
template <typename Unit> |
| 4623 |
static bool CheckNot(FunctionValidator<Unit>& f, ParseNode* expr, Type* type) { |
| 4624 |
MOZ_ASSERT(expr->isKind(ParseNodeKind::NotExpr)); |
| 4625 |
ParseNode* operand = UnaryKid(expr); |
| 4626 |
|
| 4627 |
Type operandType; |
| 4628 |
if (!CheckExpr(f, operand, &operandType)) { |
| 4629 |
return false; |
| 4630 |
} |
| 4631 |
|
| 4632 |
if (!operandType.isInt()) { |
| 4633 |
return f.failf(operand, "%s is not a subtype of int", |
| 4634 |
operandType.toChars()); |
| 4635 |
} |
| 4636 |
|
| 4637 |
*type = Type::Int; |
| 4638 |
return f.encoder().writeOp(Op::I32Eqz); |
| 4639 |
} |
| 4640 |
|
| 4641 |
template <typename Unit> |
| 4642 |
static bool CheckNeg(FunctionValidator<Unit>& f, ParseNode* expr, Type* type) { |
| 4643 |
MOZ_ASSERT(expr->isKind(ParseNodeKind::NegExpr)); |
| 4644 |
ParseNode* operand = UnaryKid(expr); |
| 4645 |
|
| 4646 |
Type operandType; |
| 4647 |
if (!CheckExpr(f, operand, &operandType)) { |
| 4648 |
return false; |
| 4649 |
} |
| 4650 |
|
| 4651 |
if (operandType.isInt()) { |
| 4652 |
*type = Type::Intish; |
| 4653 |
return f.encoder().writeOp(MozOp::I32Neg); |
| 4654 |
} |
| 4655 |
|
| 4656 |
if (operandType.isMaybeDouble()) { |
| 4657 |
*type = Type::Double; |
| 4658 |
return f.encoder().writeOp(Op::F64Neg); |
| 4659 |
} |
| 4660 |
|
| 4661 |
if (operandType.isMaybeFloat()) { |
| 4662 |
*type = Type::Floatish; |
| 4663 |
return f.encoder().writeOp(Op::F32Neg); |
| 4664 |
} |
| 4665 |
|
| 4666 |
return f.failf(operand, "%s is not a subtype of int, float? or double?", |
| 4667 |
operandType.toChars()); |
| 4668 |
} |
| 4669 |
|
| 4670 |
template <typename Unit> |
| 4671 |
static bool CheckCoerceToInt(FunctionValidator<Unit>& f, ParseNode* expr, |
| 4672 |
Type* type) { |
| 4673 |
MOZ_ASSERT(expr->isKind(ParseNodeKind::BitNotExpr)); |
| 4674 |
ParseNode* operand = UnaryKid(expr); |
| 4675 |
|
| 4676 |
Type operandType; |
| 4677 |
if (!CheckExpr(f, operand, &operandType)) { |
| 4678 |
return false; |
| 4679 |
} |
| 4680 |
|
| 4681 |
if (operandType.isMaybeDouble() || operandType.isMaybeFloat()) { |
| 4682 |
*type = Type::Signed; |
| 4683 |
Op opcode = |
| 4684 |
operandType.isMaybeDouble() ? Op::I32TruncF64S : Op::I32TruncF32S; |
| 4685 |
if (!f.prepareCall(expr)) { |
| 4686 |
return false; |
| 4687 |
} |
| 4688 |
return f.encoder().writeOp(opcode); |
| 4689 |
} |
| 4690 |
|
| 4691 |
if (!operandType.isIntish()) { |
| 4692 |
return f.failf(operand, "%s is not a subtype of double?, float? or intish", |
| 4693 |
operandType.toChars()); |
| 4694 |
} |
| 4695 |
|
| 4696 |
*type = Type::Signed; |
| 4697 |
return true; |
| 4698 |
} |
| 4699 |
|
| 4700 |
template <typename Unit> |
| 4701 |
static bool CheckBitNot(FunctionValidator<Unit>& f, ParseNode* neg, |
| 4702 |
Type* type) { |
| 4703 |
MOZ_ASSERT(neg->isKind(ParseNodeKind::BitNotExpr)); |
| 4704 |
ParseNode* operand = UnaryKid(neg); |
| 4705 |
|
| 4706 |
if (operand->isKind(ParseNodeKind::BitNotExpr)) { |
| 4707 |
return CheckCoerceToInt(f, operand, type); |
| 4708 |
} |
| 4709 |
|
| 4710 |
Type operandType; |
| 4711 |
if (!CheckExpr(f, operand, &operandType)) { |
| 4712 |
return false; |
| 4713 |
} |
| 4714 |
|
| 4715 |
if (!operandType.isIntish()) { |
| 4716 |
return f.failf(operand, "%s is not a subtype of intish", |
| 4717 |
operandType.toChars()); |
| 4718 |
} |
| 4719 |
|
| 4720 |
if (!f.encoder().writeOp(MozOp::I32BitNot)) { |
| 4721 |
return false; |
| 4722 |
} |
| 4723 |
|
| 4724 |
*type = Type::Signed; |
| 4725 |
return true; |
| 4726 |
} |
| 4727 |
|
| 4728 |
template <typename Unit> |
| 4729 |
static bool CheckAsExprStatement(FunctionValidator<Unit>& f, |
| 4730 |
ParseNode* exprStmt); |
| 4731 |
|
| 4732 |
template <typename Unit> |
| 4733 |
static bool CheckComma(FunctionValidator<Unit>& f, ParseNode* comma, |
| 4734 |
Type* type) { |
| 4735 |
MOZ_ASSERT(comma->isKind(ParseNodeKind::CommaExpr)); |
| 4736 |
ParseNode* operands = ListHead(comma); |
| 4737 |
|
| 4738 |
// The block depth isn't taken into account here, because a comma list can't |
| 4739 |
// contain breaks and continues and nested control flow structures. |
| 4740 |
if (!f.encoder().writeOp(Op::Block)) { |
| 4741 |
return false; |
| 4742 |
} |
| 4743 |
|
| 4744 |
size_t typeAt; |
| 4745 |
if (!f.encoder().writePatchableFixedU7(&typeAt)) { |
| 4746 |
return false; |
| 4747 |
} |
| 4748 |
|
| 4749 |
ParseNode* pn = operands; |
| 4750 |
for (; NextNode(pn); pn = NextNode(pn)) { |
| 4751 |
if (!CheckAsExprStatement(f, pn)) { |
| 4752 |
return false; |
| 4753 |
} |
| 4754 |
} |
| 4755 |
|
| 4756 |
if (!CheckExpr(f, pn, type)) { |
| 4757 |
return false; |
| 4758 |
} |
| 4759 |
|
| 4760 |
f.encoder().patchFixedU7(typeAt, uint8_t(type->toWasmBlockSignatureType())); |
| 4761 |
|
| 4762 |
return f.encoder().writeOp(Op::End); |
| 4763 |
} |
| 4764 |
|
| 4765 |
template <typename Unit> |
| 4766 |
static bool CheckConditional(FunctionValidator<Unit>& f, ParseNode* ternary, |
| 4767 |
Type* type) { |
| 4768 |
MOZ_ASSERT(ternary->isKind(ParseNodeKind::ConditionalExpr)); |
| 4769 |
|
| 4770 |
ParseNode* cond = TernaryKid1(ternary); |
| 4771 |
ParseNode* thenExpr = TernaryKid2(ternary); |
| 4772 |
ParseNode* elseExpr = TernaryKid3(ternary); |
| 4773 |
|
| 4774 |
Type condType; |
| 4775 |
if (!CheckExpr(f, cond, &condType)) { |
| 4776 |
return false; |
| 4777 |
} |
| 4778 |
|
| 4779 |
if (!condType.isInt()) { |
| 4780 |
return f.failf(cond, "%s is not a subtype of int", condType.toChars()); |
| 4781 |
} |
| 4782 |
|
| 4783 |
size_t typeAt; |
| 4784 |
if (!f.pushIf(&typeAt)) { |
| 4785 |
return false; |
| 4786 |
} |
| 4787 |
|
| 4788 |
Type thenType; |
| 4789 |
if (!CheckExpr(f, thenExpr, &thenType)) { |
| 4790 |
return false; |
| 4791 |
} |
| 4792 |
|
| 4793 |
if (!f.switchToElse()) { |
| 4794 |
return false; |
| 4795 |
} |
| 4796 |
|
| 4797 |
Type elseType; |
| 4798 |
if (!CheckExpr(f, elseExpr, &elseType)) { |
| 4799 |
return false; |
| 4800 |
} |
| 4801 |
|
| 4802 |
if (thenType.isInt() && elseType.isInt()) { |
| 4803 |
*type = Type::Int; |
| 4804 |
} else if (thenType.isDouble() && elseType.isDouble()) { |
| 4805 |
*type = Type::Double; |
| 4806 |
} else if (thenType.isFloat() && elseType.isFloat()) { |
| 4807 |
*type = Type::Float; |
| 4808 |
} else { |
| 4809 |
return f.failf( |
| 4810 |
ternary, |
| 4811 |
"then/else branches of conditional must both produce int, float, " |
| 4812 |
"double, current types are %s and %s", |
| 4813 |
thenType.toChars(), elseType.toChars()); |
| 4814 |
} |
| 4815 |
|
| 4816 |
return f.popIf(typeAt, type->toWasmBlockSignatureType()); |
| 4817 |
} |
| 4818 |
|
| 4819 |
template <typename Unit> |
| 4820 |
static bool IsValidIntMultiplyConstant(ModuleValidator<Unit>& m, |
| 4821 |
ParseNode* expr) { |
| 4822 |
if (!IsNumericLiteral(m, expr)) { |
| 4823 |
return false; |
| 4824 |
} |
| 4825 |
|
| 4826 |
NumLit lit = ExtractNumericLiteral(m, expr); |
| 4827 |
switch (lit.which()) { |
| 4828 |
case NumLit::Fixnum: |
| 4829 |
case NumLit::NegativeInt: |
| 4830 |
if (Abs(lit.toInt32()) < (uint32_t(1) << 20)) { |
| 4831 |
return true; |
| 4832 |
} |
| 4833 |
return false; |
| 4834 |
case NumLit::BigUnsigned: |
| 4835 |
case NumLit::Double: |
| 4836 |
case NumLit::Float: |
| 4837 |
case NumLit::OutOfRangeInt: |
| 4838 |
return false; |
| 4839 |
} |
| 4840 |
|
| 4841 |
MOZ_CRASH("Bad literal"); |
| 4842 |
} |
| 4843 |
|
| 4844 |
template <typename Unit> |
| 4845 |
static bool CheckMultiply(FunctionValidator<Unit>& f, ParseNode* star, |
| 4846 |
Type* type) { |
| 4847 |
MOZ_ASSERT(star->isKind(ParseNodeKind::MulExpr)); |
| 4848 |
ParseNode* lhs = MultiplyLeft(star); |
| 4849 |
ParseNode* rhs = MultiplyRight(star); |
| 4850 |
|
| 4851 |
Type lhsType; |
| 4852 |
if (!CheckExpr(f, lhs, &lhsType)) { |
| 4853 |
return false; |
| 4854 |
} |
| 4855 |
|
| 4856 |
Type rhsType; |
| 4857 |
if (!CheckExpr(f, rhs, &rhsType)) { |
| 4858 |
return false; |
| 4859 |
} |
| 4860 |
|
| 4861 |
if (lhsType.isInt() && rhsType.isInt()) { |
| 4862 |
if (!IsValidIntMultiplyConstant(f.m(), lhs) && |
| 4863 |
!IsValidIntMultiplyConstant(f.m(), rhs)) { |
| 4864 |
return f.fail( |
| 4865 |
star, |
| 4866 |
"one arg to int multiply must be a small (-2^20, 2^20) int literal"); |
| 4867 |
} |
| 4868 |
*type = Type::Intish; |
| 4869 |
return f.encoder().writeOp(Op::I32Mul); |
| 4870 |
} |
| 4871 |
|
| 4872 |
if (lhsType.isMaybeDouble() && rhsType.isMaybeDouble()) { |
| 4873 |
*type = Type::Double; |
| 4874 |
return f.encoder().writeOp(Op::F64Mul); |
| 4875 |
} |
| 4876 |
|
| 4877 |
if (lhsType.isMaybeFloat() && rhsType.isMaybeFloat()) { |
| 4878 |
*type = Type::Floatish; |
| 4879 |
return f.encoder().writeOp(Op::F32Mul); |
| 4880 |
} |
| 4881 |
|
| 4882 |
return f.fail( |
| 4883 |
star, "multiply operands must be both int, both double? or both float?"); |
| 4884 |
} |
| 4885 |
|
| 4886 |
template <typename Unit> |
| 4887 |
static bool CheckAddOrSub(FunctionValidator<Unit>& f, ParseNode* expr, |
| 4888 |
Type* type, unsigned* numAddOrSubOut = nullptr) { |
| 4889 |
AutoCheckRecursionLimit recursion(f.fc()); |
| 4890 |
if (!recursion.checkDontReport(f.fc())) { |
| 4891 |
return f.m().failOverRecursed(); |
| 4892 |
} |
| 4893 |
|
| 4894 |
MOZ_ASSERT(expr->isKind(ParseNodeKind::AddExpr) || |
| 4895 |
expr->isKind(ParseNodeKind::SubExpr)); |
| 4896 |
ParseNode* lhs = AddSubLeft(expr); |
| 4897 |
ParseNode* rhs = AddSubRight(expr); |
| 4898 |
|
| 4899 |
Type lhsType, rhsType; |
| 4900 |
unsigned lhsNumAddOrSub, rhsNumAddOrSub; |
| 4901 |
|
| 4902 |
if (lhs->isKind(ParseNodeKind::AddExpr) || |
| 4903 |
lhs->isKind(ParseNodeKind::SubExpr)) { |
| 4904 |
if (!CheckAddOrSub(f, lhs, &lhsType, &lhsNumAddOrSub)) { |
| 4905 |
return false; |
| 4906 |
} |
| 4907 |
if (lhsType == Type::Intish) { |
| 4908 |
lhsType = Type::Int; |
| 4909 |
} |
| 4910 |
} else { |
| 4911 |
if (!CheckExpr(f, lhs, &lhsType)) { |
| 4912 |
return false; |
| 4913 |
} |
| 4914 |
lhsNumAddOrSub = 0; |
| 4915 |
} |
| 4916 |
|
| 4917 |
if (rhs->isKind(ParseNodeKind::AddExpr) || |
| 4918 |
rhs->isKind(ParseNodeKind::SubExpr)) { |
| 4919 |
if (!CheckAddOrSub(f, rhs, &rhsType, &rhsNumAddOrSub)) { |
| 4920 |
return false; |
| 4921 |
} |
| 4922 |
if (rhsType == Type::Intish) { |
| 4923 |
rhsType = Type::Int; |
| 4924 |
} |
| 4925 |
} else { |
| 4926 |
if (!CheckExpr(f, rhs, &rhsType)) { |
| 4927 |
return false; |
| 4928 |
} |
| 4929 |
rhsNumAddOrSub = 0; |
| 4930 |
} |
| 4931 |
|
| 4932 |
unsigned numAddOrSub = lhsNumAddOrSub + rhsNumAddOrSub + 1; |
| 4933 |
if (numAddOrSub > (1 << 20)) { |
| 4934 |
return f.fail(expr, "too many + or - without intervening coercion"); |
| 4935 |
} |
| 4936 |
|
| 4937 |
if (lhsType.isInt() && rhsType.isInt()) { |
| 4938 |
if (!f.encoder().writeOp( |
| 4939 |
expr->isKind(ParseNodeKind::AddExpr) ? Op::I32Add : Op::I32Sub)) { |
| 4940 |
return false; |
| 4941 |
} |
| 4942 |
*type = Type::Intish; |
| 4943 |
} else if (lhsType.isMaybeDouble() && rhsType.isMaybeDouble()) { |
| 4944 |
if (!f.encoder().writeOp( |
| 4945 |
expr->isKind(ParseNodeKind::AddExpr) ? Op::F64Add : Op::F64Sub)) { |
| 4946 |
return false; |
| 4947 |
} |
| 4948 |
*type = Type::Double; |
| 4949 |
} else if (lhsType.isMaybeFloat() && rhsType.isMaybeFloat()) { |
| 4950 |
if (!f.encoder().writeOp( |
| 4951 |
expr->isKind(ParseNodeKind::AddExpr) ? Op::F32Add : Op::F32Sub)) { |
| 4952 |
return false; |
| 4953 |
} |
| 4954 |
*type = Type::Floatish; |
| 4955 |
} else { |
| 4956 |
return f.failf( |
| 4957 |
expr, |
| 4958 |
"operands to + or - must both be int, float? or double?, got %s and %s", |
| 4959 |
lhsType.toChars(), rhsType.toChars()); |
| 4960 |
} |
| 4961 |
|
| 4962 |
if (numAddOrSubOut) { |
| 4963 |
*numAddOrSubOut = numAddOrSub; |
| 4964 |
} |
| 4965 |
return true; |
| 4966 |
} |
| 4967 |
|
| 4968 |
template <typename Unit> |
| 4969 |
static bool CheckDivOrMod(FunctionValidator<Unit>& f, ParseNode* expr, |
| 4970 |
Type* type) { |
| 4971 |
MOZ_ASSERT(expr->isKind(ParseNodeKind::DivExpr) || |
| 4972 |
expr->isKind(ParseNodeKind::ModExpr)); |
| 4973 |
|
| 4974 |
ParseNode* lhs = DivOrModLeft(expr); |
| 4975 |
ParseNode* rhs = DivOrModRight(expr); |
| 4976 |
|
| 4977 |
Type lhsType, rhsType; |
| 4978 |
if (!CheckExpr(f, lhs, &lhsType)) { |
| 4979 |
return false; |
| 4980 |
} |
| 4981 |
if (!CheckExpr(f, rhs, &rhsType)) { |
| 4982 |
return false; |
| 4983 |
} |
| 4984 |
|
| 4985 |
if (lhsType.isMaybeDouble() && rhsType.isMaybeDouble()) { |
| 4986 |
*type = Type::Double; |
| 4987 |
if (expr->isKind(ParseNodeKind::DivExpr)) { |
| 4988 |
return f.encoder().writeOp(Op::F64Div); |
| 4989 |
} |
| 4990 |
return f.encoder().writeOp(MozOp::F64Mod); |
| 4991 |
} |
| 4992 |
|
| 4993 |
if (lhsType.isMaybeFloat() && rhsType.isMaybeFloat()) { |
| 4994 |
*type = Type::Floatish; |
| 4995 |
if (expr->isKind(ParseNodeKind::DivExpr)) { |
| 4996 |
return f.encoder().writeOp(Op::F32Div); |
| 4997 |
} |
| 4998 |
return f.fail(expr, "modulo cannot receive float arguments"); |
| 4999 |
} |
| 5000 |
|
| 5001 |
if (lhsType.isSigned() && rhsType.isSigned()) { |
| 5002 |
*type = Type::Intish; |
| 5003 |
return f.encoder().writeOp( |
| 5004 |
expr->isKind(ParseNodeKind::DivExpr) ? Op::I32DivS : Op::I32RemS); |
| 5005 |
} |
| 5006 |
|
| 5007 |
if (lhsType.isUnsigned() && rhsType.isUnsigned()) { |
| 5008 |
*type = Type::Intish; |
| 5009 |
return f.encoder().writeOp( |
| 5010 |
expr->isKind(ParseNodeKind::DivExpr) ? Op::I32DivU : Op::I32RemU); |
| 5011 |
} |
| 5012 |
|
| 5013 |
return f.failf( |
| 5014 |
expr, |
| 5015 |
"arguments to / or %% must both be double?, float?, signed, or unsigned; " |
| 5016 |
"%s and %s are given", |
| 5017 |
lhsType.toChars(), rhsType.toChars()); |
| 5018 |
} |
| 5019 |
|
| 5020 |
template <typename Unit> |
| 5021 |
static bool CheckComparison(FunctionValidator<Unit>& f, ParseNode* comp, |
| 5022 |
Type* type) { |
| 5023 |
MOZ_ASSERT(comp->isKind(ParseNodeKind::LtExpr) || |
| 5024 |
comp->isKind(ParseNodeKind::LeExpr) || |
| 5025 |
comp->isKind(ParseNodeKind::GtExpr) || |
| 5026 |
comp->isKind(ParseNodeKind::GeExpr) || |
| 5027 |
comp->isKind(ParseNodeKind::EqExpr) || |
| 5028 |
comp->isKind(ParseNodeKind::NeExpr)); |
| 5029 |
|
| 5030 |
ParseNode* lhs = ComparisonLeft(comp); |
| 5031 |
ParseNode* rhs = ComparisonRight(comp); |
| 5032 |
|
| 5033 |
Type lhsType, rhsType; |
| 5034 |
if (!CheckExpr(f, lhs, &lhsType)) { |
| 5035 |
return false; |
| 5036 |
} |
| 5037 |
if (!CheckExpr(f, rhs, &rhsType)) { |
| 5038 |
return false; |
| 5039 |
} |
| 5040 |
|
| 5041 |
if (!(lhsType.isSigned() && rhsType.isSigned()) && |
| 5042 |
!(lhsType.isUnsigned() && rhsType.isUnsigned()) && |
| 5043 |
!(lhsType.isDouble() && rhsType.isDouble()) && |
| 5044 |
!(lhsType.isFloat() && rhsType.isFloat())) { |
| 5045 |
return f.failf(comp, |
| 5046 |
"arguments to a comparison must both be signed, unsigned, " |
| 5047 |
"floats or doubles; " |
| 5048 |
"%s and %s are given", |
| 5049 |
lhsType.toChars(), rhsType.toChars()); |
| 5050 |
} |
| 5051 |
|
| 5052 |
Op stmt; |
| 5053 |
if (lhsType.isSigned() && rhsType.isSigned()) { |
| 5054 |
switch (comp->getKind()) { |
| 5055 |
case ParseNodeKind::EqExpr: |
| 5056 |
stmt = Op::I32Eq; |
| 5057 |
break; |
| 5058 |
case ParseNodeKind::NeExpr: |
| 5059 |
stmt = Op::I32Ne; |
| 5060 |
break; |
| 5061 |
case ParseNodeKind::LtExpr: |
| 5062 |
stmt = Op::I32LtS; |
| 5063 |
break; |
| 5064 |
case ParseNodeKind::LeExpr: |
| 5065 |
stmt = Op::I32LeS; |
| 5066 |
break; |
| 5067 |
case ParseNodeKind::GtExpr: |
| 5068 |
stmt = Op::I32GtS; |
| 5069 |
break; |
| 5070 |
case ParseNodeKind::GeExpr: |
| 5071 |
stmt = Op::I32GeS; |
| 5072 |
break; |
| 5073 |
default: |
| 5074 |
MOZ_CRASH("unexpected comparison op"); |
| 5075 |
} |
| 5076 |
} else if (lhsType.isUnsigned() && rhsType.isUnsigned()) { |
| 5077 |
switch (comp->getKind()) { |
| 5078 |
case ParseNodeKind::EqExpr: |
| 5079 |
stmt = Op::I32Eq; |
| 5080 |
break; |
| 5081 |
case ParseNodeKind::NeExpr: |
| 5082 |
stmt = Op::I32Ne; |
| 5083 |
break; |
| 5084 |
case ParseNodeKind::LtExpr: |
| 5085 |
stmt = Op::I32LtU; |
| 5086 |
break; |
| 5087 |
case ParseNodeKind::LeExpr: |
| 5088 |
stmt = Op::I32LeU; |
| 5089 |
break; |
| 5090 |
case ParseNodeKind::GtExpr: |
| 5091 |
stmt = Op::I32GtU; |
| 5092 |
break; |
| 5093 |
case ParseNodeKind::GeExpr: |
| 5094 |
stmt = Op::I32GeU; |
| 5095 |
break; |
| 5096 |
default: |
| 5097 |
MOZ_CRASH("unexpected comparison op"); |
| 5098 |
} |
| 5099 |
} else if (lhsType.isDouble()) { |
| 5100 |
switch (comp->getKind()) { |
| 5101 |
case ParseNodeKind::EqExpr: |
| 5102 |
stmt = Op::F64Eq; |
| 5103 |
break; |
| 5104 |
case ParseNodeKind::NeExpr: |
| 5105 |
stmt = Op::F64Ne; |
| 5106 |
break; |
| 5107 |
case ParseNodeKind::LtExpr: |
| 5108 |
stmt = Op::F64Lt; |
| 5109 |
break; |
| 5110 |
case ParseNodeKind::LeExpr: |
| 5111 |
stmt = Op::F64Le; |
| 5112 |
break; |
| 5113 |
case ParseNodeKind::GtExpr: |
| 5114 |
stmt = Op::F64Gt; |
| 5115 |
break; |
| 5116 |
case ParseNodeKind::GeExpr: |
| 5117 |
stmt = Op::F64Ge; |
| 5118 |
break; |
| 5119 |
default: |
| 5120 |
MOZ_CRASH("unexpected comparison op"); |
| 5121 |
} |
| 5122 |
} else if (lhsType.isFloat()) { |
| 5123 |
switch (comp->getKind()) { |
| 5124 |
case ParseNodeKind::EqExpr: |
| 5125 |
stmt = Op::F32Eq; |
| 5126 |
break; |
| 5127 |
case ParseNodeKind::NeExpr: |
| 5128 |
stmt = Op::F32Ne; |
| 5129 |
break; |
| 5130 |
case ParseNodeKind::LtExpr: |
| 5131 |
stmt = Op::F32Lt; |
| 5132 |
break; |
| 5133 |
case ParseNodeKind::LeExpr: |
| 5134 |
stmt = Op::F32Le; |
| 5135 |
break; |
| 5136 |
case ParseNodeKind::GtExpr: |
| 5137 |
stmt = Op::F32Gt; |
| 5138 |
break; |
| 5139 |
case ParseNodeKind::GeExpr: |
| 5140 |
stmt = Op::F32Ge; |
| 5141 |
break; |
| 5142 |
default: |
| 5143 |
MOZ_CRASH("unexpected comparison op"); |
| 5144 |
} |
| 5145 |
} else { |
| 5146 |
MOZ_CRASH("unexpected type"); |
| 5147 |
} |
| 5148 |
|
| 5149 |
*type = Type::Int; |
| 5150 |
return f.encoder().writeOp(stmt); |
| 5151 |
} |
| 5152 |
|
| 5153 |
template <typename Unit> |
| 5154 |
static bool CheckBitwise(FunctionValidator<Unit>& f, ParseNode* bitwise, |
| 5155 |
Type* type) { |
| 5156 |
ParseNode* lhs = BitwiseLeft(bitwise); |
| 5157 |
ParseNode* rhs = BitwiseRight(bitwise); |
| 5158 |
|
| 5159 |
int32_t identityElement; |
| 5160 |
bool onlyOnRight; |
| 5161 |
switch (bitwise->getKind()) { |
| 5162 |
case ParseNodeKind::BitOrExpr: |
| 5163 |
identityElement = 0; |
| 5164 |
onlyOnRight = false; |
| 5165 |
*type = Type::Signed; |
| 5166 |
break; |
| 5167 |
case ParseNodeKind::BitAndExpr: |
| 5168 |
identityElement = -1; |
| 5169 |
onlyOnRight = false; |
| 5170 |
*type = Type::Signed; |
| 5171 |
break; |
| 5172 |
case ParseNodeKind::BitXorExpr: |
| 5173 |
identityElement = 0; |
| 5174 |
onlyOnRight = false; |
| 5175 |
*type = Type::Signed; |
| 5176 |
break; |
| 5177 |
case ParseNodeKind::LshExpr: |
| 5178 |
identityElement = 0; |
| 5179 |
onlyOnRight = true; |
| 5180 |
*type = Type::Signed; |
| 5181 |
break; |
| 5182 |
case ParseNodeKind::RshExpr: |
| 5183 |
identityElement = 0; |
| 5184 |
onlyOnRight = true; |
| 5185 |
*type = Type::Signed; |
| 5186 |
break; |
| 5187 |
case ParseNodeKind::UrshExpr: |
| 5188 |
identityElement = 0; |
| 5189 |
onlyOnRight = true; |
| 5190 |
*type = Type::Unsigned; |
| 5191 |
break; |
| 5192 |
default: |
| 5193 |
MOZ_CRASH("not a bitwise op"); |
| 5194 |
} |
| 5195 |
|
| 5196 |
uint32_t i; |
| 5197 |
if (!onlyOnRight && IsLiteralInt(f.m(), lhs, &i) && |
| 5198 |
i == uint32_t(identityElement)) { |
| 5199 |
Type rhsType; |
| 5200 |
if (!CheckExpr(f, rhs, &rhsType)) { |
| 5201 |
return false; |
| 5202 |
} |
| 5203 |
if (!rhsType.isIntish()) { |
| 5204 |
return f.failf(bitwise, "%s is not a subtype of intish", |
| 5205 |
rhsType.toChars()); |
| 5206 |
} |
| 5207 |
return true; |
| 5208 |
} |
| 5209 |
|
| 5210 |
if (IsLiteralInt(f.m(), rhs, &i) && i == uint32_t(identityElement)) { |
| 5211 |
if (bitwise->isKind(ParseNodeKind::BitOrExpr) && |
| 5212 |
lhs->isKind(ParseNodeKind::CallExpr)) { |
| 5213 |
return CheckCoercedCall(f, lhs, Type::Int, type); |
| 5214 |
} |
| 5215 |
|
| 5216 |
Type lhsType; |
| 5217 |
if (!CheckExpr(f, lhs, &lhsType)) { |
| 5218 |
return false; |
| 5219 |
} |
| 5220 |
if (!lhsType.isIntish()) { |
| 5221 |
return f.failf(bitwise, "%s is not a subtype of intish", |
| 5222 |
lhsType.toChars()); |
| 5223 |
} |
| 5224 |
return true; |
| 5225 |
} |
| 5226 |
|
| 5227 |
Type lhsType; |
| 5228 |
if (!CheckExpr(f, lhs, &lhsType)) { |
| 5229 |
return false; |
| 5230 |
} |
| 5231 |
|
| 5232 |
Type rhsType; |
| 5233 |
if (!CheckExpr(f, rhs, &rhsType)) { |
| 5234 |
return false; |
| 5235 |
} |
| 5236 |
|
| 5237 |
if (!lhsType.isIntish()) { |
| 5238 |
return f.failf(lhs, "%s is not a subtype of intish", lhsType.toChars()); |
| 5239 |
} |
| 5240 |
if (!rhsType.isIntish()) { |
| 5241 |
return f.failf(rhs, "%s is not a subtype of intish", rhsType.toChars()); |
| 5242 |
} |
| 5243 |
|
| 5244 |
switch (bitwise->getKind()) { |
| 5245 |
case ParseNodeKind::BitOrExpr: |
| 5246 |
if (!f.encoder().writeOp(Op::I32Or)) return false; |
| 5247 |
break; |
| 5248 |
case ParseNodeKind::BitAndExpr: |
| 5249 |
if (!f.encoder().writeOp(Op::I32And)) return false; |
| 5250 |
break; |
| 5251 |
case ParseNodeKind::BitXorExpr: |
| 5252 |
if (!f.encoder().writeOp(Op::I32Xor)) return false; |
| 5253 |
break; |
| 5254 |
case ParseNodeKind::LshExpr: |
| 5255 |
if (!f.encoder().writeOp(Op::I32Shl)) return false; |
| 5256 |
break; |
| 5257 |
case ParseNodeKind::RshExpr: |
| 5258 |
if (!f.encoder().writeOp(Op::I32ShrS)) return false; |
| 5259 |
break; |
| 5260 |
case ParseNodeKind::UrshExpr: |
| 5261 |
if (!f.encoder().writeOp(Op::I32ShrU)) return false; |
| 5262 |
break; |
| 5263 |
default: |
| 5264 |
MOZ_CRASH("not a bitwise op"); |
| 5265 |
} |
| 5266 |
|
| 5267 |
return true; |
| 5268 |
} |
| 5269 |
|
| 5270 |
template <typename Unit> |
| 5271 |
static bool CheckExpr(FunctionValidator<Unit>& f, ParseNode* expr, Type* type) { |
| 5272 |
AutoCheckRecursionLimit recursion(f.fc()); |
| 5273 |
if (!recursion.checkDontReport(f.fc())) { |
| 5274 |
return f.m().failOverRecursed(); |
| 5275 |
} |
| 5276 |
|
| 5277 |
if (IsNumericLiteral(f.m(), expr)) { |
| 5278 |
return CheckNumericLiteral(f, expr, type); |
| 5279 |
} |
| 5280 |
|
| 5281 |
switch (expr->getKind()) { |
| 5282 |
case ParseNodeKind::Name: |
| 5283 |
return CheckVarRef(f, expr, type); |
| 5284 |
case ParseNodeKind::ElemExpr: |
| 5285 |
return CheckLoadArray(f, expr, type); |
| 5286 |
case ParseNodeKind::AssignExpr: |
| 5287 |
return CheckAssign(f, expr, type); |
| 5288 |
case ParseNodeKind::PosExpr: |
| 5289 |
return CheckPos(f, expr, type); |
| 5290 |
case ParseNodeKind::NotExpr: |
| 5291 |
return CheckNot(f, expr, type); |
| 5292 |
case ParseNodeKind::NegExpr: |
| 5293 |
return CheckNeg(f, expr, type); |
| 5294 |
case ParseNodeKind::BitNotExpr: |
| 5295 |
return CheckBitNot(f, expr, type); |
| 5296 |
case ParseNodeKind::CommaExpr: |
| 5297 |
return CheckComma(f, expr, type); |
| 5298 |
case ParseNodeKind::ConditionalExpr: |
| 5299 |
return CheckConditional(f, expr, type); |
| 5300 |
case ParseNodeKind::MulExpr: |
| 5301 |
return CheckMultiply(f, expr, type); |
| 5302 |
case ParseNodeKind::CallExpr: |
| 5303 |
return CheckUncoercedCall(f, expr, type); |
| 5304 |
|
| 5305 |
case ParseNodeKind::AddExpr: |
| 5306 |
case ParseNodeKind::SubExpr: |
| 5307 |
return CheckAddOrSub(f, expr, type); |
| 5308 |
|
| 5309 |
case ParseNodeKind::DivExpr: |
| 5310 |
case ParseNodeKind::ModExpr: |
| 5311 |
return CheckDivOrMod(f, expr, type); |
| 5312 |
|
| 5313 |
case ParseNodeKind::LtExpr: |
| 5314 |
case ParseNodeKind::LeExpr: |
| 5315 |
case ParseNodeKind::GtExpr: |
| 5316 |
case ParseNodeKind::GeExpr: |
| 5317 |
case ParseNodeKind::EqExpr: |
| 5318 |
case ParseNodeKind::NeExpr: |
| 5319 |
return CheckComparison(f, expr, type); |
| 5320 |
|
| 5321 |
case ParseNodeKind::BitOrExpr: |
| 5322 |
case ParseNodeKind::BitAndExpr: |
| 5323 |
case ParseNodeKind::BitXorExpr: |
| 5324 |
case ParseNodeKind::LshExpr: |
| 5325 |
case ParseNodeKind::RshExpr: |
| 5326 |
case ParseNodeKind::UrshExpr: |
| 5327 |
return CheckBitwise(f, expr, type); |
| 5328 |
|
| 5329 |
default:; |
| 5330 |
} |
| 5331 |
|
| 5332 |
return f.fail(expr, "unsupported expression"); |
| 5333 |
} |
| 5334 |
|
| 5335 |
template <typename Unit> |
| 5336 |
static bool CheckStatement(FunctionValidator<Unit>& f, ParseNode* stmt); |
| 5337 |
|
| 5338 |
template <typename Unit> |
| 5339 |
static bool CheckAsExprStatement(FunctionValidator<Unit>& f, ParseNode* expr) { |
| 5340 |
if (expr->isKind(ParseNodeKind::CallExpr)) { |
| 5341 |
Type ignored; |
| 5342 |
return CheckCoercedCall(f, expr, Type::Void, &ignored); |
| 5343 |
} |
| 5344 |
|
| 5345 |
Type resultType; |
| 5346 |
if (!CheckExpr(f, expr, &resultType)) { |
| 5347 |
return false; |
| 5348 |
} |
| 5349 |
|
| 5350 |
if (!resultType.isVoid()) { |
| 5351 |
if (!f.encoder().writeOp(Op::Drop)) { |
| 5352 |
return false; |
| 5353 |
} |
| 5354 |
} |
| 5355 |
|
| 5356 |
return true; |
| 5357 |
} |
| 5358 |
|
| 5359 |
template <typename Unit> |
| 5360 |
static bool CheckExprStatement(FunctionValidator<Unit>& f, |
| 5361 |
ParseNode* exprStmt) { |
| 5362 |
MOZ_ASSERT(exprStmt->isKind(ParseNodeKind::ExpressionStmt)); |
| 5363 |
return CheckAsExprStatement(f, UnaryKid(exprStmt)); |
| 5364 |
} |
| 5365 |
|
| 5366 |
template <typename Unit> |
| 5367 |
static bool CheckLoopConditionOnEntry(FunctionValidator<Unit>& f, |
| 5368 |
ParseNode* cond) { |
| 5369 |
uint32_t maybeLit; |
| 5370 |
if (IsLiteralInt(f.m(), cond, &maybeLit) && maybeLit) { |
| 5371 |
return true; |
| 5372 |
} |
| 5373 |
|
| 5374 |
Type condType; |
| 5375 |
if (!CheckExpr(f, cond, &condType)) { |
| 5376 |
return false; |
| 5377 |
} |
| 5378 |
if (!condType.isInt()) { |
| 5379 |
return f.failf(cond, "%s is not a subtype of int", condType.toChars()); |
| 5380 |
} |
| 5381 |
|
| 5382 |
if (!f.encoder().writeOp(Op::I32Eqz)) { |
| 5383 |
return false; |
| 5384 |
} |
| 5385 |
|
| 5386 |
// brIf (i32.eqz $f) $out |
| 5387 |
return f.writeBreakIf(); |
| 5388 |
} |
| 5389 |
|
| 5390 |
template <typename Unit> |
| 5391 |
static bool CheckWhile(FunctionValidator<Unit>& f, ParseNode* whileStmt, |
| 5392 |
const LabelVector* labels = nullptr) { |
| 5393 |
MOZ_ASSERT(whileStmt->isKind(ParseNodeKind::WhileStmt)); |
| 5394 |
ParseNode* cond = BinaryLeft(whileStmt); |
| 5395 |
ParseNode* body = BinaryRight(whileStmt); |
| 5396 |
|
| 5397 |
// A while loop `while(#cond) #body` is equivalent to: |
| 5398 |
// (block $after_loop |
| 5399 |
// (loop $top |
| 5400 |
// (brIf $after_loop (i32.eq 0 #cond)) |
| 5401 |
// #body |
| 5402 |
// (br $top) |
| 5403 |
// ) |
| 5404 |
// ) |
| 5405 |
if (labels && !f.addLabels(*labels, 0, 1)) { |
| 5406 |
return false; |
| 5407 |
} |
| 5408 |
|
| 5409 |
if (!f.pushLoop()) { |
| 5410 |
return false; |
| 5411 |
} |
| 5412 |
|
| 5413 |
if (!CheckLoopConditionOnEntry(f, cond)) { |
| 5414 |
return false; |
| 5415 |
} |
| 5416 |
if (!CheckStatement(f, body)) { |
| 5417 |
return false; |
| 5418 |
} |
| 5419 |
if (!f.writeContinue()) { |
| 5420 |
return false; |
| 5421 |
} |
| 5422 |
|
| 5423 |
if (!f.popLoop()) { |
| 5424 |
return false; |
| 5425 |
} |
| 5426 |
if (labels) { |
| 5427 |
f.removeLabels(*labels); |
| 5428 |
} |
| 5429 |
return true; |
| 5430 |
} |
| 5431 |
|
| 5432 |
template <typename Unit> |
| 5433 |
static bool CheckFor(FunctionValidator<Unit>& f, ParseNode* forStmt, |
| 5434 |
const LabelVector* labels = nullptr) { |
| 5435 |
MOZ_ASSERT(forStmt->isKind(ParseNodeKind::ForStmt)); |
| 5436 |
ParseNode* forHead = BinaryLeft(forStmt); |
| 5437 |
ParseNode* body = BinaryRight(forStmt); |
| 5438 |
|
| 5439 |
if (!forHead->isKind(ParseNodeKind::ForHead)) { |
| 5440 |
return f.fail(forHead, "unsupported for-loop statement"); |
| 5441 |
} |
| 5442 |
|
| 5443 |
ParseNode* maybeInit = TernaryKid1(forHead); |
| 5444 |
ParseNode* maybeCond = TernaryKid2(forHead); |
| 5445 |
ParseNode* maybeInc = TernaryKid3(forHead); |
| 5446 |
|
| 5447 |
// A for-loop `for (#init; #cond; #inc) #body` is equivalent to: |
| 5448 |
// (block // depth X |
| 5449 |
// (#init) |
| 5450 |
// (block $after_loop // depth X+1 (block) |
| 5451 |
// (loop $loop_top // depth X+2 (loop) |
| 5452 |
// (brIf $after (eq 0 #cond)) |
| 5453 |
// (block $after_body #body) // depth X+3 |
| 5454 |
// #inc |
| 5455 |
// (br $loop_top) |
| 5456 |
// ) |
| 5457 |
// ) |
| 5458 |
// ) |
| 5459 |
// A break in the body should break out to $after_loop, i.e. depth + 1. |
| 5460 |
// A continue in the body should break out to $after_body, i.e. depth + 3. |
| 5461 |
if (labels && !f.addLabels(*labels, 1, 3)) { |
| 5462 |
return false; |
| 5463 |
} |
| 5464 |
|
| 5465 |
if (!f.pushUnbreakableBlock()) { |
| 5466 |
return false; |
| 5467 |
} |
| 5468 |
|
| 5469 |
if (maybeInit && !CheckAsExprStatement(f, maybeInit)) { |
| 5470 |
return false; |
| 5471 |
} |
| 5472 |
|
| 5473 |
{ |
| 5474 |
if (!f.pushLoop()) { |
| 5475 |
return false; |
| 5476 |
} |
| 5477 |
|
| 5478 |
if (maybeCond && !CheckLoopConditionOnEntry(f, maybeCond)) { |
| 5479 |
return false; |
| 5480 |
} |
| 5481 |
|
| 5482 |
{ |
| 5483 |
// Continuing in the body should just break out to the increment. |
| 5484 |
if (!f.pushContinuableBlock()) { |
| 5485 |
return false; |
| 5486 |
} |
| 5487 |
if (!CheckStatement(f, body)) { |
| 5488 |
return false; |
| 5489 |
} |
| 5490 |
if (!f.popContinuableBlock()) { |
| 5491 |
return false; |
| 5492 |
} |
| 5493 |
} |
| 5494 |
|
| 5495 |
if (maybeInc && !CheckAsExprStatement(f, maybeInc)) { |
| 5496 |
return false; |
| 5497 |
} |
| 5498 |
|
| 5499 |
if (!f.writeContinue()) { |
| 5500 |
return false; |
| 5501 |
} |
| 5502 |
if (!f.popLoop()) { |
| 5503 |
return false; |
| 5504 |
} |
| 5505 |
} |
| 5506 |
|
| 5507 |
if (!f.popUnbreakableBlock()) { |
| 5508 |
return false; |
| 5509 |
} |
| 5510 |
|
| 5511 |
if (labels) { |
| 5512 |
f.removeLabels(*labels); |
| 5513 |
} |
| 5514 |
|
| 5515 |
return true; |
| 5516 |
} |
| 5517 |
|
| 5518 |
template <typename Unit> |
| 5519 |
static bool CheckDoWhile(FunctionValidator<Unit>& f, ParseNode* whileStmt, |
| 5520 |
const LabelVector* labels = nullptr) { |
| 5521 |
MOZ_ASSERT(whileStmt->isKind(ParseNodeKind::DoWhileStmt)); |
| 5522 |
ParseNode* body = BinaryLeft(whileStmt); |
| 5523 |
ParseNode* cond = BinaryRight(whileStmt); |
| 5524 |
|
| 5525 |
// A do-while loop `do { #body } while (#cond)` is equivalent to: |
| 5526 |
// (block $after_loop // depth X |
| 5527 |
// (loop $top // depth X+1 |
| 5528 |
// (block #body) // depth X+2 |
| 5529 |
// (brIf #cond $top) |
| 5530 |
// ) |
| 5531 |
// ) |
| 5532 |
// A break should break out of the entire loop, i.e. at depth 0. |
| 5533 |
// A continue should break out to the condition, i.e. at depth 2. |
| 5534 |
if (labels && !f.addLabels(*labels, 0, 2)) { |
| 5535 |
return false; |
| 5536 |
} |
| 5537 |
|
| 5538 |
if (!f.pushLoop()) { |
| 5539 |
return false; |
| 5540 |
} |
| 5541 |
|
| 5542 |
{ |
| 5543 |
// An unlabeled continue in the body should break out to the condition. |
| 5544 |
if (!f.pushContinuableBlock()) { |
| 5545 |
return false; |
| 5546 |
} |
| 5547 |
if (!CheckStatement(f, body)) { |
| 5548 |
return false; |
| 5549 |
} |
| 5550 |
if (!f.popContinuableBlock()) { |
| 5551 |
return false; |
| 5552 |
} |
| 5553 |
} |
| 5554 |
|
| 5555 |
Type condType; |
| 5556 |
if (!CheckExpr(f, cond, &condType)) { |
| 5557 |
return false; |
| 5558 |
} |
| 5559 |
if (!condType.isInt()) { |
| 5560 |
return f.failf(cond, "%s is not a subtype of int", condType.toChars()); |
| 5561 |
} |
| 5562 |
|
| 5563 |
if (!f.writeContinueIf()) { |
| 5564 |
return false; |
| 5565 |
} |
| 5566 |
|
| 5567 |
if (!f.popLoop()) { |
| 5568 |
return false; |
| 5569 |
} |
| 5570 |
if (labels) { |
| 5571 |
f.removeLabels(*labels); |
| 5572 |
} |
| 5573 |
return true; |
| 5574 |
} |
| 5575 |
|
| 5576 |
template <typename Unit> |
| 5577 |
static bool CheckStatementList(FunctionValidator<Unit>& f, ParseNode*, |
| 5578 |
const LabelVector* = nullptr); |
| 5579 |
|
| 5580 |
template <typename Unit> |
| 5581 |
static bool CheckLabel(FunctionValidator<Unit>& f, ParseNode* labeledStmt) { |
| 5582 |
MOZ_ASSERT(labeledStmt->isKind(ParseNodeKind::LabelStmt)); |
| 5583 |
|
| 5584 |
LabelVector labels; |
| 5585 |
ParseNode* innermost = labeledStmt; |
| 5586 |
do { |
| 5587 |
if (!labels.append(LabeledStatementLabel(innermost))) { |
| 5588 |
return false; |
| 5589 |
} |
| 5590 |
innermost = LabeledStatementStatement(innermost); |
| 5591 |
} while (innermost->getKind() == ParseNodeKind::LabelStmt); |
| 5592 |
|
| 5593 |
switch (innermost->getKind()) { |
| 5594 |
case ParseNodeKind::ForStmt: |
| 5595 |
return CheckFor(f, innermost, &labels); |
| 5596 |
case ParseNodeKind::DoWhileStmt: |
| 5597 |
return CheckDoWhile(f, innermost, &labels); |
| 5598 |
case ParseNodeKind::WhileStmt: |
| 5599 |
return CheckWhile(f, innermost, &labels); |
| 5600 |
case ParseNodeKind::StatementList: |
| 5601 |
return CheckStatementList(f, innermost, &labels); |
| 5602 |
default: |
| 5603 |
break; |
| 5604 |
} |
| 5605 |
|
| 5606 |
if (!f.pushUnbreakableBlock(&labels)) { |
| 5607 |
return false; |
| 5608 |
} |
| 5609 |
|
| 5610 |
if (!CheckStatement(f, innermost)) { |
| 5611 |
return false; |
| 5612 |
} |
| 5613 |
|
| 5614 |
return f.popUnbreakableBlock(&labels); |
| 5615 |
} |
| 5616 |
|
| 5617 |
template <typename Unit> |
| 5618 |
static bool CheckIf(FunctionValidator<Unit>& f, ParseNode* ifStmt) { |
| 5619 |
uint32_t numIfEnd = 1; |
| 5620 |
|
| 5621 |
recurse: |
| 5622 |
MOZ_ASSERT(ifStmt->isKind(ParseNodeKind::IfStmt)); |
| 5623 |
ParseNode* cond = TernaryKid1(ifStmt); |
| 5624 |
ParseNode* thenStmt = TernaryKid2(ifStmt); |
| 5625 |
ParseNode* elseStmt = TernaryKid3(ifStmt); |
| 5626 |
|
| 5627 |
Type condType; |
| 5628 |
if (!CheckExpr(f, cond, &condType)) { |
| 5629 |
return false; |
| 5630 |
} |
| 5631 |
if (!condType.isInt()) { |
| 5632 |
return f.failf(cond, "%s is not a subtype of int", condType.toChars()); |
| 5633 |
} |
| 5634 |
|
| 5635 |
size_t typeAt; |
| 5636 |
if (!f.pushIf(&typeAt)) { |
| 5637 |
return false; |
| 5638 |
} |
| 5639 |
|
| 5640 |
f.setIfType(typeAt, TypeCode::BlockVoid); |
| 5641 |
|
| 5642 |
if (!CheckStatement(f, thenStmt)) { |
| 5643 |
return false; |
| 5644 |
} |
| 5645 |
|
| 5646 |
if (elseStmt) { |
| 5647 |
if (!f.switchToElse()) { |
| 5648 |
return false; |
| 5649 |
} |
| 5650 |
|
| 5651 |
if (elseStmt->isKind(ParseNodeKind::IfStmt)) { |
| 5652 |
ifStmt = elseStmt; |
| 5653 |
if (numIfEnd++ == UINT32_MAX) { |
| 5654 |
return false; |
| 5655 |
} |
| 5656 |
goto recurse; |
| 5657 |
} |
| 5658 |
|
| 5659 |
if (!CheckStatement(f, elseStmt)) { |
| 5660 |
return false; |
| 5661 |
} |
| 5662 |
} |
| 5663 |
|
| 5664 |
for (uint32_t i = 0; i != numIfEnd; ++i) { |
| 5665 |
if (!f.popIf()) { |
| 5666 |
return false; |
| 5667 |
} |
| 5668 |
} |
| 5669 |
|
| 5670 |
return true; |
| 5671 |
} |
| 5672 |
|
| 5673 |
static bool CheckCaseExpr(FunctionValidatorShared& f, ParseNode* caseExpr, |
| 5674 |
int32_t* value) { |
| 5675 |
if (!IsNumericLiteral(f.m(), caseExpr)) { |
| 5676 |
return f.fail(caseExpr, |
| 5677 |
"switch case expression must be an integer literal"); |
| 5678 |
} |
| 5679 |
|
| 5680 |
NumLit lit = ExtractNumericLiteral(f.m(), caseExpr); |
| 5681 |
switch (lit.which()) { |
| 5682 |
case NumLit::Fixnum: |
| 5683 |
case NumLit::NegativeInt: |
| 5684 |
*value = lit.toInt32(); |
| 5685 |
break; |
| 5686 |
case NumLit::OutOfRangeInt: |
| 5687 |
case NumLit::BigUnsigned: |
| 5688 |
return f.fail(caseExpr, "switch case expression out of integer range"); |
| 5689 |
case NumLit::Double: |
| 5690 |
case NumLit::Float: |
| 5691 |
return f.fail(caseExpr, |
| 5692 |
"switch case expression must be an integer literal"); |
| 5693 |
} |
| 5694 |
|
| 5695 |
return true; |
| 5696 |
} |
| 5697 |
|
| 5698 |
static bool CheckDefaultAtEnd(FunctionValidatorShared& f, ParseNode* stmt) { |
| 5699 |
for (; stmt; stmt = NextNode(stmt)) { |
| 5700 |
if (IsDefaultCase(stmt) && NextNode(stmt) != nullptr) { |
| 5701 |
return f.fail(stmt, "default label must be at the end"); |
| 5702 |
} |
| 5703 |
} |
| 5704 |
|
| 5705 |
return true; |
| 5706 |
} |
| 5707 |
|
| 5708 |
static bool CheckSwitchRange(FunctionValidatorShared& f, ParseNode* stmt, |
| 5709 |
int32_t* low, int32_t* high, |
| 5710 |
uint32_t* tableLength) { |
| 5711 |
if (IsDefaultCase(stmt)) { |
| 5712 |
*low = 0; |
| 5713 |
*high = -1; |
| 5714 |
*tableLength = 0; |
| 5715 |
return true; |
| 5716 |
} |
| 5717 |
|
| 5718 |
int32_t i = 0; |
| 5719 |
if (!CheckCaseExpr(f, CaseExpr(stmt), &i)) { |
| 5720 |
return false; |
| 5721 |
} |
| 5722 |
|
| 5723 |
*low = *high = i; |
| 5724 |
|
| 5725 |
ParseNode* initialStmt = stmt; |
| 5726 |
for (stmt = NextNode(stmt); stmt && !IsDefaultCase(stmt); |
| 5727 |
stmt = NextNode(stmt)) { |
| 5728 |
int32_t i = 0; |
| 5729 |
if (!CheckCaseExpr(f, CaseExpr(stmt), &i)) { |
| 5730 |
return false; |
| 5731 |
} |
| 5732 |
|
| 5733 |
*low = std::min(*low, i); |
| 5734 |
*high = std::max(*high, i); |
| 5735 |
} |
| 5736 |
|
| 5737 |
int64_t i64 = (int64_t(*high) - int64_t(*low)) + 1; |
| 5738 |
if (i64 > MaxBrTableElems) { |
| 5739 |
return f.fail( |
| 5740 |
initialStmt, |
| 5741 |
"all switch statements generate tables; this table would be too big"); |
| 5742 |
} |
| 5743 |
|
| 5744 |
*tableLength = uint32_t(i64); |
| 5745 |
return true; |
| 5746 |
} |
| 5747 |
|
| 5748 |
template <typename Unit> |
| 5749 |
static bool CheckSwitchExpr(FunctionValidator<Unit>& f, ParseNode* switchExpr) { |
| 5750 |
Type exprType; |
| 5751 |
if (!CheckExpr(f, switchExpr, &exprType)) { |
| 5752 |
return false; |
| 5753 |
} |
| 5754 |
if (!exprType.isSigned()) { |
| 5755 |
return f.failf(switchExpr, "%s is not a subtype of signed", |
| 5756 |
exprType.toChars()); |
| 5757 |
} |
| 5758 |
return true; |
| 5759 |
} |
| 5760 |
|
| 5761 |
// A switch will be constructed as: |
| 5762 |
// - the default block wrapping all the other blocks, to be able to break |
| 5763 |
// out of the switch with an unlabeled break statement. It has two statements |
| 5764 |
// (an inner block and the default expr). asm.js rules require default to be at |
| 5765 |
// the end, so the default block always encloses all the cases blocks. |
| 5766 |
// - one block per case between low and high; undefined cases just jump to the |
| 5767 |
// default case. Each of these blocks contain two statements: the next case's |
| 5768 |
// block and the possibly empty statement list comprising the case body. The |
| 5769 |
// last block pushed is the first case so the (relative) branch target therefore |
| 5770 |
// matches the sequential order of cases. |
| 5771 |
// - one block for the br_table, so that the first break goes to the first |
| 5772 |
// case's block. |
| 5773 |
template <typename Unit> |
| 5774 |
static bool CheckSwitch(FunctionValidator<Unit>& f, ParseNode* switchStmt) { |
| 5775 |
MOZ_ASSERT(switchStmt->isKind(ParseNodeKind::SwitchStmt)); |
| 5776 |
|
| 5777 |
ParseNode* switchExpr = BinaryLeft(switchStmt); |
| 5778 |
ParseNode* switchBody = BinaryRight(switchStmt); |
| 5779 |
|
| 5780 |
if (switchBody->is<LexicalScopeNode>()) { |
| 5781 |
LexicalScopeNode* scope = &switchBody->as<LexicalScopeNode>(); |
| 5782 |
if (!scope->isEmptyScope()) { |
| 5783 |
return f.fail(scope, "switch body may not contain lexical declarations"); |
| 5784 |
} |
| 5785 |
switchBody = scope->scopeBody(); |
| 5786 |
} |
| 5787 |
|
| 5788 |
ParseNode* stmt = ListHead(switchBody); |
| 5789 |
if (!stmt) { |
| 5790 |
if (!CheckSwitchExpr(f, switchExpr)) { |
| 5791 |
return false; |
| 5792 |
} |
| 5793 |
return f.encoder().writeOp(Op::Drop); |
| 5794 |
} |
| 5795 |
|
| 5796 |
if (!CheckDefaultAtEnd(f, stmt)) { |
| 5797 |
return false; |
| 5798 |
} |
| 5799 |
|
| 5800 |
int32_t low = 0, high = 0; |
| 5801 |
uint32_t tableLength = 0; |
| 5802 |
if (!CheckSwitchRange(f, stmt, &low, &high, &tableLength)) { |
| 5803 |
return false; |
| 5804 |
} |
| 5805 |
|
| 5806 |
static const uint32_t CASE_NOT_DEFINED = UINT32_MAX; |
| 5807 |
|
| 5808 |
Uint32Vector caseDepths; |
| 5809 |
if (!caseDepths.appendN(CASE_NOT_DEFINED, tableLength)) { |
| 5810 |
return false; |
| 5811 |
} |
| 5812 |
|
| 5813 |
uint32_t numCases = 0; |
| 5814 |
for (ParseNode* s = stmt; s && !IsDefaultCase(s); s = NextNode(s)) { |
| 5815 |
int32_t caseValue = ExtractNumericLiteral(f.m(), CaseExpr(s)).toInt32(); |
| 5816 |
|
| 5817 |
MOZ_ASSERT(caseValue >= low); |
| 5818 |
unsigned i = caseValue - low; |
| 5819 |
if (caseDepths[i] != CASE_NOT_DEFINED) { |
| 5820 |
return f.fail(s, "no duplicate case labels"); |
| 5821 |
} |
| 5822 |
|
| 5823 |
MOZ_ASSERT(numCases != CASE_NOT_DEFINED); |
| 5824 |
caseDepths[i] = numCases++; |
| 5825 |
} |
| 5826 |
|
| 5827 |
// Open the wrapping breakable default block. |
| 5828 |
if (!f.pushBreakableBlock()) { |
| 5829 |
return false; |
| 5830 |
} |
| 5831 |
|
| 5832 |
// Open all the case blocks. |
| 5833 |
for (uint32_t i = 0; i < numCases; i++) { |
| 5834 |
if (!f.pushUnbreakableBlock()) { |
| 5835 |
return false; |
| 5836 |
} |
| 5837 |
} |
| 5838 |
|
| 5839 |
// Open the br_table block. |
| 5840 |
if (!f.pushUnbreakableBlock()) { |
| 5841 |
return false; |
| 5842 |
} |
| 5843 |
|
| 5844 |
// The default block is the last one. |
| 5845 |
uint32_t defaultDepth = numCases; |
| 5846 |
|
| 5847 |
// Subtract lowest case value, so that all the cases start from 0. |
| 5848 |
if (low) { |
| 5849 |
if (!CheckSwitchExpr(f, switchExpr)) { |
| 5850 |
return false; |
| 5851 |
} |
| 5852 |
if (!f.writeInt32Lit(low)) { |
| 5853 |
return false; |
| 5854 |
} |
| 5855 |
if (!f.encoder().writeOp(Op::I32Sub)) { |
| 5856 |
return false; |
| 5857 |
} |
| 5858 |
} else { |
| 5859 |
if (!CheckSwitchExpr(f, switchExpr)) { |
| 5860 |
return false; |
| 5861 |
} |
| 5862 |
} |
| 5863 |
|
| 5864 |
// Start the br_table block. |
| 5865 |
if (!f.encoder().writeOp(Op::BrTable)) { |
| 5866 |
return false; |
| 5867 |
} |
| 5868 |
|
| 5869 |
// Write the number of cases (tableLength - 1 + 1 (default)). |
| 5870 |
// Write the number of cases (tableLength - 1 + 1 (default)). |
| 5871 |
if (!f.encoder().writeVarU32(tableLength)) { |
| 5872 |
return false; |
| 5873 |
} |
| 5874 |
|
| 5875 |
// Each case value describes the relative depth to the actual block. When |
| 5876 |
// a case is not explicitly defined, it goes to the default. |
| 5877 |
for (size_t i = 0; i < tableLength; i++) { |
| 5878 |
uint32_t target = |
| 5879 |
caseDepths[i] == CASE_NOT_DEFINED ? defaultDepth : caseDepths[i]; |
| 5880 |
if (!f.encoder().writeVarU32(target)) { |
| 5881 |
return false; |
| 5882 |
} |
| 5883 |
} |
| 5884 |
|
| 5885 |
// Write the default depth. |
| 5886 |
if (!f.encoder().writeVarU32(defaultDepth)) { |
| 5887 |
return false; |
| 5888 |
} |
| 5889 |
|
| 5890 |
// Our br_table is done. Close its block, write the cases down in order. |
| 5891 |
if (!f.popUnbreakableBlock()) { |
| 5892 |
return false; |
| 5893 |
} |
| 5894 |
|
| 5895 |
for (; stmt && !IsDefaultCase(stmt); stmt = NextNode(stmt)) { |
| 5896 |
if (!CheckStatement(f, CaseBody(stmt))) { |
| 5897 |
return false; |
| 5898 |
} |
| 5899 |
if (!f.popUnbreakableBlock()) { |
| 5900 |
return false; |
| 5901 |
} |
| 5902 |
} |
| 5903 |
|
| 5904 |
// Write the default block. |
| 5905 |
if (stmt && IsDefaultCase(stmt)) { |
| 5906 |
if (!CheckStatement(f, CaseBody(stmt))) { |
| 5907 |
return false; |
| 5908 |
} |
| 5909 |
} |
| 5910 |
|
| 5911 |
// Close the wrapping block. |
| 5912 |
return f.popBreakableBlock(); |
| 5913 |
} |
| 5914 |
|
| 5915 |
static bool CheckReturnType(FunctionValidatorShared& f, ParseNode* usepn, |
| 5916 |
Type ret) { |
| 5917 |
Maybe<ValType> type = ret.canonicalToReturnType(); |
| 5918 |
|
| 5919 |
if (!f.hasAlreadyReturned()) { |
| 5920 |
f.setReturnedType(type); |
| 5921 |
return true; |
| 5922 |
} |
| 5923 |
|
| 5924 |
if (f.returnedType() != type) { |
| 5925 |
return f.failf(usepn, "%s incompatible with previous return of type %s", |
| 5926 |
ToString(type, nullptr).get(), |
| 5927 |
ToString(f.returnedType(), nullptr).get()); |
| 5928 |
} |
| 5929 |
|
| 5930 |
return true; |
| 5931 |
} |
| 5932 |
|
| 5933 |
template <typename Unit> |
| 5934 |
static bool CheckReturn(FunctionValidator<Unit>& f, ParseNode* returnStmt) { |
| 5935 |
ParseNode* expr = ReturnExpr(returnStmt); |
| 5936 |
|
| 5937 |
if (!expr) { |
| 5938 |
if (!CheckReturnType(f, returnStmt, Type::Void)) { |
| 5939 |
return false; |
| 5940 |
} |
| 5941 |
} else { |
| 5942 |
Type type; |
| 5943 |
if (!CheckExpr(f, expr, &type)) { |
| 5944 |
return false; |
| 5945 |
} |
| 5946 |
|
| 5947 |
if (!type.isReturnType()) { |
| 5948 |
return f.failf(expr, "%s is not a valid return type", type.toChars()); |
| 5949 |
} |
| 5950 |
|
| 5951 |
if (!CheckReturnType(f, expr, Type::canonicalize(type))) { |
| 5952 |
return false; |
| 5953 |
} |
| 5954 |
} |
| 5955 |
|
| 5956 |
return f.encoder().writeOp(Op::Return); |
| 5957 |
} |
| 5958 |
|
| 5959 |
template <typename Unit> |
| 5960 |
static bool CheckStatementList(FunctionValidator<Unit>& f, ParseNode* stmtList, |
| 5961 |
const LabelVector* labels /*= nullptr */) { |
| 5962 |
MOZ_ASSERT(stmtList->isKind(ParseNodeKind::StatementList)); |
| 5963 |
|
| 5964 |
if (!f.pushUnbreakableBlock(labels)) { |
| 5965 |
return false; |
| 5966 |
} |
| 5967 |
|
| 5968 |
for (ParseNode* stmt = ListHead(stmtList); stmt; stmt = NextNode(stmt)) { |
| 5969 |
if (!CheckStatement(f, stmt)) { |
| 5970 |
return false; |
| 5971 |
} |
| 5972 |
} |
| 5973 |
|
| 5974 |
return f.popUnbreakableBlock(labels); |
| 5975 |
} |
| 5976 |
|
| 5977 |
template <typename Unit> |
| 5978 |
static bool CheckLexicalScope(FunctionValidator<Unit>& f, ParseNode* node) { |
| 5979 |
LexicalScopeNode* lexicalScope = &node->as<LexicalScopeNode>(); |
| 5980 |
if (!lexicalScope->isEmptyScope()) { |
| 5981 |
return f.fail(lexicalScope, "cannot have 'let' or 'const' declarations"); |
| 5982 |
} |
| 5983 |
|
| 5984 |
return CheckStatement(f, lexicalScope->scopeBody()); |
| 5985 |
} |
| 5986 |
|
| 5987 |
static bool CheckBreakOrContinue(FunctionValidatorShared& f, bool isBreak, |
| 5988 |
ParseNode* stmt) { |
| 5989 |
if (TaggedParserAtomIndex maybeLabel = LoopControlMaybeLabel(stmt)) { |
| 5990 |
return f.writeLabeledBreakOrContinue(maybeLabel, isBreak); |
| 5991 |
} |
| 5992 |
return f.writeUnlabeledBreakOrContinue(isBreak); |
| 5993 |
} |
| 5994 |
|
| 5995 |
template <typename Unit> |
| 5996 |
static bool CheckStatement(FunctionValidator<Unit>& f, ParseNode* stmt) { |
| 5997 |
AutoCheckRecursionLimit recursion(f.fc()); |
| 5998 |
if (!recursion.checkDontReport(f.fc())) { |
| 5999 |
return f.m().failOverRecursed(); |
| 6000 |
} |
| 6001 |
|
| 6002 |
switch (stmt->getKind()) { |
| 6003 |
case ParseNodeKind::EmptyStmt: |
| 6004 |
return true; |
| 6005 |
case ParseNodeKind::ExpressionStmt: |
| 6006 |
return CheckExprStatement(f, stmt); |
| 6007 |
case ParseNodeKind::WhileStmt: |
| 6008 |
return CheckWhile(f, stmt); |
| 6009 |
case ParseNodeKind::ForStmt: |
| 6010 |
return CheckFor(f, stmt); |
| 6011 |
case ParseNodeKind::DoWhileStmt: |
| 6012 |
return CheckDoWhile(f, stmt); |
| 6013 |
case ParseNodeKind::LabelStmt: |
| 6014 |
return CheckLabel(f, stmt); |
| 6015 |
case ParseNodeKind::IfStmt: |
| 6016 |
return CheckIf(f, stmt); |
| 6017 |
case ParseNodeKind::SwitchStmt: |
| 6018 |
return CheckSwitch(f, stmt); |
| 6019 |
case ParseNodeKind::ReturnStmt: |
| 6020 |
return CheckReturn(f, stmt); |
| 6021 |
case ParseNodeKind::StatementList: |
| 6022 |
return CheckStatementList(f, stmt); |
| 6023 |
case ParseNodeKind::BreakStmt: |
| 6024 |
return CheckBreakOrContinue(f, true, stmt); |
| 6025 |
case ParseNodeKind::ContinueStmt: |
| 6026 |
return CheckBreakOrContinue(f, false, stmt); |
| 6027 |
case ParseNodeKind::LexicalScope: |
| 6028 |
return CheckLexicalScope(f, stmt); |
| 6029 |
default:; |
| 6030 |
} |
| 6031 |
|
| 6032 |
return f.fail(stmt, "unexpected statement kind"); |
| 6033 |
} |
| 6034 |
|
| 6035 |
template <typename Unit> |
| 6036 |
static bool ParseFunction(ModuleValidator<Unit>& m, FunctionNode** funNodeOut, |
| 6037 |
unsigned* line) { |
| 6038 |
auto& tokenStream = m.tokenStream(); |
| 6039 |
|
| 6040 |
tokenStream.consumeKnownToken(TokenKind::Function, |
| 6041 |
TokenStreamShared::SlashIsRegExp); |
| 6042 |
|
| 6043 |
auto& anyChars = tokenStream.anyCharsAccess(); |
| 6044 |
uint32_t toStringStart = anyChars.currentToken().pos.begin; |
| 6045 |
*line = anyChars.lineNumber(anyChars.lineToken(toStringStart)); |
| 6046 |
|
| 6047 |
TokenKind tk; |
| 6048 |
if (!tokenStream.getToken(&tk, TokenStreamShared::SlashIsRegExp)) { |
| 6049 |
return false; |
| 6050 |
} |
| 6051 |
if (tk == TokenKind::Mul) { |
| 6052 |
return m.failCurrentOffset("unexpected generator function"); |
| 6053 |
} |
| 6054 |
if (!TokenKindIsPossibleIdentifier(tk)) { |
| 6055 |
return false; // The regular parser will throw a SyntaxError, no need to |
| 6056 |
// m.fail. |
| 6057 |
} |
| 6058 |
|
| 6059 |
TaggedParserAtomIndex name = m.parser().bindingIdentifier(YieldIsName); |
| 6060 |
if (!name) { |
| 6061 |
return false; |
| 6062 |
} |
| 6063 |
|
| 6064 |
FunctionNode* funNode; |
| 6065 |
MOZ_TRY_VAR_OR_RETURN(funNode, |
| 6066 |
m.parser().handler_.newFunction( |
| 6067 |
FunctionSyntaxKind::Statement, m.parser().pos()), |
| 6068 |
false); |
| 6069 |
|
| 6070 |
ParseContext* outerpc = m.parser().pc_; |
| 6071 |
Directives directives(outerpc); |
| 6072 |
FunctionFlags flags(FunctionFlags::INTERPRETED_NORMAL); |
| 6073 |
FunctionBox* funbox = m.parser().newFunctionBox( |
| 6074 |
funNode, name, flags, toStringStart, directives, |
| 6075 |
GeneratorKind::NotGenerator, FunctionAsyncKind::SyncFunction); |
| 6076 |
if (!funbox) { |
| 6077 |
return false; |
| 6078 |
} |
| 6079 |
funbox->initWithEnclosingParseContext(outerpc, FunctionSyntaxKind::Statement); |
| 6080 |
|
| 6081 |
Directives newDirectives = directives; |
| 6082 |
SourceParseContext funpc(&m.parser(), funbox, &newDirectives); |
| 6083 |
if (!funpc.init()) { |
| 6084 |
return false; |
| 6085 |
} |
| 6086 |
|
| 6087 |
if (!m.parser().functionFormalParametersAndBody( |
| 6088 |
InAllowed, YieldIsName, &funNode, FunctionSyntaxKind::Statement)) { |
| 6089 |
if (anyChars.hadError() || directives == newDirectives) { |
| 6090 |
return false; |
| 6091 |
} |
| 6092 |
|
| 6093 |
return m.fail(funNode, "encountered new directive in function"); |
| 6094 |
} |
| 6095 |
|
| 6096 |
MOZ_ASSERT(!anyChars.hadError()); |
| 6097 |
MOZ_ASSERT(directives == newDirectives); |
| 6098 |
|
| 6099 |
*funNodeOut = funNode; |
| 6100 |
return true; |
| 6101 |
} |
| 6102 |
|
| 6103 |
template <typename Unit> |
| 6104 |
static bool CheckFunction(ModuleValidator<Unit>& m) { |
| 6105 |
// asm.js modules can be quite large when represented as parse trees so pop |
| 6106 |
// the backing LifoAlloc after parsing/compiling each function. Release the |
| 6107 |
// parser's lifo memory after the last use of a parse node. |
| 6108 |
frontend::ParserBase::Mark mark = m.parser().mark(); |
| 6109 |
auto releaseMark = |
| 6110 |
mozilla::MakeScopeExit([&m, &mark] { m.parser().release(mark); }); |
| 6111 |
|
| 6112 |
FunctionNode* funNode = nullptr; |
| 6113 |
unsigned line = 0; |
| 6114 |
if (!ParseFunction(m, &funNode, &line)) { |
| 6115 |
return false; |
| 6116 |
} |
| 6117 |
|
| 6118 |
if (!CheckFunctionHead(m, funNode)) { |
| 6119 |
return false; |
| 6120 |
} |
| 6121 |
|
| 6122 |
FunctionValidator<Unit> f(m, funNode); |
| 6123 |
|
| 6124 |
ParseNode* stmtIter = ListHead(FunctionStatementList(funNode)); |
| 6125 |
|
| 6126 |
if (!CheckProcessingDirectives(m, &stmtIter)) { |
| 6127 |
return false; |
| 6128 |
} |
| 6129 |
|
| 6130 |
ValTypeVector args; |
| 6131 |
if (!CheckArguments(f, &stmtIter, &args)) { |
| 6132 |
return false; |
| 6133 |
} |
| 6134 |
|
| 6135 |
if (!CheckVariables(f, &stmtIter)) { |
| 6136 |
return false; |
| 6137 |
} |
| 6138 |
|
| 6139 |
ParseNode* lastNonEmptyStmt = nullptr; |
| 6140 |
for (; stmtIter; stmtIter = NextNonEmptyStatement(stmtIter)) { |
| 6141 |
lastNonEmptyStmt = stmtIter; |
| 6142 |
if (!CheckStatement(f, stmtIter)) { |
| 6143 |
return false; |
| 6144 |
} |
| 6145 |
} |
| 6146 |
|
| 6147 |
if (!CheckFinalReturn(f, lastNonEmptyStmt)) { |
| 6148 |
return false; |
| 6149 |
} |
| 6150 |
|
| 6151 |
ValTypeVector results; |
| 6152 |
if (f.returnedType()) { |
| 6153 |
if (!results.append(f.returnedType().ref())) { |
| 6154 |
return false; |
| 6155 |
} |
| 6156 |
} |
| 6157 |
|
| 6158 |
FuncType sig(std::move(args), std::move(results)); |
| 6159 |
|
| 6160 |
ModuleValidatorShared::Func* func = nullptr; |
| 6161 |
if (!CheckFunctionSignature(m, funNode, std::move(sig), FunctionName(funNode), |
| 6162 |
&func)) { |
| 6163 |
return false; |
| 6164 |
} |
| 6165 |
|
| 6166 |
if (func->defined()) { |
| 6167 |
return m.failName(funNode, "function '%s' already defined", |
| 6168 |
FunctionName(funNode)); |
| 6169 |
} |
| 6170 |
|
| 6171 |
f.define(func, line); |
| 6172 |
|
| 6173 |
return true; |
| 6174 |
} |
| 6175 |
|
| 6176 |
static bool CheckAllFunctionsDefined(ModuleValidatorShared& m) { |
| 6177 |
for (unsigned i = 0; i < m.numFuncDefs(); i++) { |
| 6178 |
const ModuleValidatorShared::Func& f = m.funcDef(i); |
| 6179 |
if (!f.defined()) { |
| 6180 |
return m.failNameOffset(f.firstUse(), "missing definition of function %s", |
| 6181 |
f.name()); |
| 6182 |
} |
| 6183 |
} |
| 6184 |
|
| 6185 |
return true; |
| 6186 |
} |
| 6187 |
|
| 6188 |
template <typename Unit> |
| 6189 |
static bool CheckFunctions(ModuleValidator<Unit>& m) { |
| 6190 |
while (true) { |
| 6191 |
TokenKind tk; |
| 6192 |
if (!PeekToken(m.parser(), &tk)) { |
| 6193 |
return false; |
| 6194 |
} |
| 6195 |
|
| 6196 |
if (tk != TokenKind::Function) { |
| 6197 |
break; |
| 6198 |
} |
| 6199 |
|
| 6200 |
if (!CheckFunction(m)) { |
| 6201 |
return false; |
| 6202 |
} |
| 6203 |
} |
| 6204 |
|
| 6205 |
return CheckAllFunctionsDefined(m); |
| 6206 |
} |
| 6207 |
|
| 6208 |
template <typename Unit> |
| 6209 |
static bool CheckFuncPtrTable(ModuleValidator<Unit>& m, ParseNode* decl) { |
| 6210 |
if (!decl->isKind(ParseNodeKind::AssignExpr)) { |
| 6211 |
return m.fail(decl, "function-pointer table must have initializer"); |
| 6212 |
} |
| 6213 |
AssignmentNode* assignNode = &decl->as<AssignmentNode>(); |
| 6214 |
|
| 6215 |
ParseNode* var = assignNode->left(); |
| 6216 |
|
| 6217 |
if (!var->isKind(ParseNodeKind::Name)) { |
| 6218 |
return m.fail(var, "function-pointer table name is not a plain name"); |
| 6219 |
} |
| 6220 |
|
| 6221 |
ParseNode* arrayLiteral = assignNode->right(); |
| 6222 |
|
| 6223 |
if (!arrayLiteral->isKind(ParseNodeKind::ArrayExpr)) { |
| 6224 |
return m.fail( |
| 6225 |
var, "function-pointer table's initializer must be an array literal"); |
| 6226 |
} |
| 6227 |
|
| 6228 |
unsigned length = ListLength(arrayLiteral); |
| 6229 |
|
| 6230 |
if (!IsPowerOfTwo(length)) { |
| 6231 |
return m.failf(arrayLiteral, |
| 6232 |
"function-pointer table length must be a power of 2 (is %u)", |
| 6233 |
length); |
| 6234 |
} |
| 6235 |
|
| 6236 |
unsigned mask = length - 1; |
| 6237 |
|
| 6238 |
Uint32Vector elemFuncDefIndices; |
| 6239 |
const FuncType* sig = nullptr; |
| 6240 |
for (ParseNode* elem = ListHead(arrayLiteral); elem; elem = NextNode(elem)) { |
| 6241 |
if (!elem->isKind(ParseNodeKind::Name)) { |
| 6242 |
return m.fail( |
| 6243 |
elem, "function-pointer table's elements must be names of functions"); |
| 6244 |
} |
| 6245 |
|
| 6246 |
TaggedParserAtomIndex funcName = elem->as<NameNode>().name(); |
| 6247 |
const ModuleValidatorShared::Func* func = m.lookupFuncDef(funcName); |
| 6248 |
if (!func) { |
| 6249 |
return m.fail( |
| 6250 |
elem, "function-pointer table's elements must be names of functions"); |
| 6251 |
} |
| 6252 |
|
| 6253 |
const FuncType& funcSig = |
| 6254 |
m.codeMeta()->types->type(func->sigIndex()).funcType(); |
| 6255 |
if (sig) { |
| 6256 |
if (!FuncType::strictlyEquals(*sig, funcSig)) { |
| 6257 |
return m.fail(elem, "all functions in table must have same signature"); |
| 6258 |
} |
| 6259 |
} else { |
| 6260 |
sig = &funcSig; |
| 6261 |
} |
| 6262 |
|
| 6263 |
if (!elemFuncDefIndices.append(func->funcDefIndex())) { |
| 6264 |
return false; |
| 6265 |
} |
| 6266 |
} |
| 6267 |
|
| 6268 |
FuncType copy; |
| 6269 |
if (!copy.clone(*sig)) { |
| 6270 |
return false; |
| 6271 |
} |
| 6272 |
|
| 6273 |
uint32_t tableIndex; |
| 6274 |
if (!CheckFuncPtrTableAgainstExisting(m, var, var->as<NameNode>().name(), |
| 6275 |
std::move(copy), mask, &tableIndex)) { |
| 6276 |
return false; |
| 6277 |
} |
| 6278 |
|
| 6279 |
if (!m.defineFuncPtrTable(tableIndex, std::move(elemFuncDefIndices))) { |
| 6280 |
return m.fail(var, "duplicate function-pointer definition"); |
| 6281 |
} |
| 6282 |
|
| 6283 |
return true; |
| 6284 |
} |
| 6285 |
|
| 6286 |
template <typename Unit> |
| 6287 |
static bool CheckFuncPtrTables(ModuleValidator<Unit>& m) { |
| 6288 |
while (true) { |
| 6289 |
ParseNode* varStmt; |
| 6290 |
if (!ParseVarOrConstStatement(m.parser(), &varStmt)) { |
| 6291 |
return false; |
| 6292 |
} |
| 6293 |
if (!varStmt) { |
| 6294 |
break; |
| 6295 |
} |
| 6296 |
for (ParseNode* var = VarListHead(varStmt); var; var = NextNode(var)) { |
| 6297 |
if (!CheckFuncPtrTable(m, var)) { |
| 6298 |
return false; |
| 6299 |
} |
| 6300 |
} |
| 6301 |
} |
| 6302 |
|
| 6303 |
for (unsigned i = 0; i < m.numFuncPtrTables(); i++) { |
| 6304 |
ModuleValidatorShared::Table& table = m.table(i); |
| 6305 |
if (!table.defined()) { |
| 6306 |
return m.failNameOffset(table.firstUse(), |
| 6307 |
"function-pointer table %s wasn't defined", |
| 6308 |
table.name()); |
| 6309 |
} |
| 6310 |
} |
| 6311 |
|
| 6312 |
return true; |
| 6313 |
} |
| 6314 |
|
| 6315 |
static bool CheckModuleExportFunction( |
| 6316 |
ModuleValidatorShared& m, ParseNode* pn, |
| 6317 |
TaggedParserAtomIndex maybeFieldName = TaggedParserAtomIndex::null()) { |
| 6318 |
if (!pn->isKind(ParseNodeKind::Name)) { |
| 6319 |
return m.fail(pn, "expected name of exported function"); |
| 6320 |
} |
| 6321 |
|
| 6322 |
TaggedParserAtomIndex funcName = pn->as<NameNode>().name(); |
| 6323 |
const ModuleValidatorShared::Func* func = m.lookupFuncDef(funcName); |
| 6324 |
if (!func) { |
| 6325 |
return m.failName(pn, "function '%s' not found", funcName); |
| 6326 |
} |
| 6327 |
|
| 6328 |
return m.addExportField(*func, maybeFieldName); |
| 6329 |
} |
| 6330 |
|
| 6331 |
static bool CheckModuleExportObject(ModuleValidatorShared& m, |
| 6332 |
ParseNode* object) { |
| 6333 |
MOZ_ASSERT(object->isKind(ParseNodeKind::ObjectExpr)); |
| 6334 |
|
| 6335 |
for (ParseNode* pn = ListHead(object); pn; pn = NextNode(pn)) { |
| 6336 |
if (!IsNormalObjectField(pn)) { |
| 6337 |
return m.fail(pn, |
| 6338 |
"only normal object properties may be used in the export " |
| 6339 |
"object literal"); |
| 6340 |
} |
| 6341 |
|
| 6342 |
TaggedParserAtomIndex fieldName = ObjectNormalFieldName(pn); |
| 6343 |
|
| 6344 |
ParseNode* initNode = ObjectNormalFieldInitializer(pn); |
| 6345 |
if (!initNode->isKind(ParseNodeKind::Name)) { |
| 6346 |
return m.fail( |
| 6347 |
initNode, |
| 6348 |
"initializer of exported object literal must be name of function"); |
| 6349 |
} |
| 6350 |
|
| 6351 |
if (!CheckModuleExportFunction(m, initNode, fieldName)) { |
| 6352 |
return false; |
| 6353 |
} |
| 6354 |
} |
| 6355 |
|
| 6356 |
return true; |
| 6357 |
} |
| 6358 |
|
| 6359 |
template <typename Unit> |
| 6360 |
static bool CheckModuleReturn(ModuleValidator<Unit>& m) { |
| 6361 |
TokenKind tk; |
| 6362 |
if (!GetToken(m.parser(), &tk)) { |
| 6363 |
return false; |
| 6364 |
} |
| 6365 |
auto& ts = m.parser().tokenStream; |
| 6366 |
if (tk != TokenKind::Return) { |
| 6367 |
return m.failCurrentOffset( |
| 6368 |
(tk == TokenKind::RightCurly || tk == TokenKind::Eof) |
| 6369 |
? "expecting return statement" |
| 6370 |
: "invalid asm.js. statement"); |
| 6371 |
} |
| 6372 |
ts.anyCharsAccess().ungetToken(); |
| 6373 |
|
| 6374 |
ParseNode* returnStmt; |
| 6375 |
MOZ_TRY_VAR_OR_RETURN(returnStmt, m.parser().statementListItem(YieldIsName), |
| 6376 |
false); |
| 6377 |
|
| 6378 |
ParseNode* returnExpr = ReturnExpr(returnStmt); |
| 6379 |
if (!returnExpr) { |
| 6380 |
return m.fail(returnStmt, "export statement must return something"); |
| 6381 |
} |
| 6382 |
|
| 6383 |
if (returnExpr->isKind(ParseNodeKind::ObjectExpr)) { |
| 6384 |
if (!CheckModuleExportObject(m, returnExpr)) { |
| 6385 |
return false; |
| 6386 |
} |
| 6387 |
} else { |
| 6388 |
if (!CheckModuleExportFunction(m, returnExpr)) { |
| 6389 |
return false; |
| 6390 |
} |
| 6391 |
} |
| 6392 |
|
| 6393 |
return true; |
| 6394 |
} |
| 6395 |
|
| 6396 |
template <typename Unit> |
| 6397 |
static bool CheckModuleEnd(ModuleValidator<Unit>& m) { |
| 6398 |
TokenKind tk; |
| 6399 |
if (!GetToken(m.parser(), &tk)) { |
| 6400 |
return false; |
| 6401 |
} |
| 6402 |
|
| 6403 |
if (tk != TokenKind::Eof && tk != TokenKind::RightCurly) { |
| 6404 |
return m.failCurrentOffset( |
| 6405 |
"top-level export (return) must be the last statement"); |
| 6406 |
} |
| 6407 |
|
| 6408 |
m.parser().tokenStream.anyCharsAccess().ungetToken(); |
| 6409 |
return true; |
| 6410 |
} |
| 6411 |
|
| 6412 |
template <typename Unit> |
| 6413 |
static SharedModule CheckModule(FrontendContext* fc, |
| 6414 |
ParserAtomsTable& parserAtoms, |
| 6415 |
AsmJSParser<Unit>& parser, ParseNode* stmtList, |
| 6416 |
unsigned* time) { |
| 6417 |
int64_t before = PRMJ_Now(); |
| 6418 |
|
| 6419 |
ScriptedCaller scriptedCaller; |
| 6420 |
if (parser.ss->filename()) { |
| 6421 |
scriptedCaller.line = 0; // unused |
| 6422 |
scriptedCaller.filename = DuplicateString(parser.ss->filename()); |
| 6423 |
if (!scriptedCaller.filename) { |
| 6424 |
return nullptr; |
| 6425 |
} |
| 6426 |
} |
| 6427 |
|
| 6428 |
// The default options are fine for asm.js |
| 6429 |
SharedCompileArgs args = |
| 6430 |
CompileArgs::buildForAsmJS(std::move(scriptedCaller)); |
| 6431 |
if (!args) { |
| 6432 |
ReportOutOfMemory(fc); |
| 6433 |
return nullptr; |
| 6434 |
} |
| 6435 |
|
| 6436 |
MutableModuleMetadata moduleMeta = js_new<ModuleMetadata>(); |
| 6437 |
if (!moduleMeta || !moduleMeta->init(*args, ModuleKind::AsmJS)) { |
| 6438 |
return nullptr; |
| 6439 |
} |
| 6440 |
MutableCodeMetadata codeMeta = moduleMeta->codeMeta; |
| 6441 |
|
| 6442 |
FunctionNode* moduleFunctionNode = parser.pc_->functionBox()->functionNode; |
| 6443 |
|
| 6444 |
ModuleValidator<Unit> m(fc, parserAtoms, moduleMeta, codeMeta, parser, |
| 6445 |
moduleFunctionNode); |
| 6446 |
if (!m.init()) { |
| 6447 |
return nullptr; |
| 6448 |
} |
| 6449 |
|
| 6450 |
if (!CheckFunctionHead(m, moduleFunctionNode)) { |
| 6451 |
return nullptr; |
| 6452 |
} |
| 6453 |
|
| 6454 |
if (!CheckModuleArguments(m, moduleFunctionNode)) { |
| 6455 |
return nullptr; |
| 6456 |
} |
| 6457 |
|
| 6458 |
if (!CheckPrecedingStatements(m, stmtList)) { |
| 6459 |
return nullptr; |
| 6460 |
} |
| 6461 |
|
| 6462 |
if (!CheckModuleProcessingDirectives(m)) { |
| 6463 |
return nullptr; |
| 6464 |
} |
| 6465 |
|
| 6466 |
if (!CheckModuleGlobals(m)) { |
| 6467 |
return nullptr; |
| 6468 |
} |
| 6469 |
|
| 6470 |
if (!m.startFunctionBodies()) { |
| 6471 |
return nullptr; |
| 6472 |
} |
| 6473 |
|
| 6474 |
if (!CheckFunctions(m)) { |
| 6475 |
return nullptr; |
| 6476 |
} |
| 6477 |
|
| 6478 |
if (!CheckFuncPtrTables(m)) { |
| 6479 |
return nullptr; |
| 6480 |
} |
| 6481 |
|
| 6482 |
if (!CheckModuleReturn(m)) { |
| 6483 |
return nullptr; |
| 6484 |
} |
| 6485 |
|
| 6486 |
if (!CheckModuleEnd(m)) { |
| 6487 |
return nullptr; |
| 6488 |
} |
| 6489 |
|
| 6490 |
SharedModule module = m.finish(); |
| 6491 |
if (!module) { |
| 6492 |
return nullptr; |
| 6493 |
} |
| 6494 |
|
| 6495 |
*time = (PRMJ_Now() - before) / PRMJ_USEC_PER_MSEC; |
| 6496 |
return module; |
| 6497 |
} |
| 6498 |
|
| 6499 |
/*****************************************************************************/ |
| 6500 |
// Link-time validation |
| 6501 |
|
| 6502 |
static bool LinkFail(JSContext* cx, const char* str) { |
| 6503 |
WarnNumberASCII(cx, JSMSG_USE_ASM_LINK_FAIL, str); |
| 6504 |
return false; |
| 6505 |
} |
| 6506 |
|
| 6507 |
static bool IsMaybeWrappedScriptedProxy(JSObject* obj) { |
| 6508 |
JSObject* unwrapped = UncheckedUnwrap(obj); |
| 6509 |
return unwrapped && IsScriptedProxy(unwrapped); |
| 6510 |
} |
| 6511 |
|
| 6512 |
static bool GetDataProperty(JSContext* cx, HandleValue objVal, |
| 6513 |
Handle<JSAtom*> field, MutableHandleValue v) { |
| 6514 |
if (!objVal.isObject()) { |
| 6515 |
return LinkFail(cx, "accessing property of non-object"); |
| 6516 |
} |
| 6517 |
|
| 6518 |
RootedObject obj(cx, &objVal.toObject()); |
| 6519 |
if (IsMaybeWrappedScriptedProxy(obj)) { |
| 6520 |
return LinkFail(cx, "accessing property of a Proxy"); |
| 6521 |
} |
| 6522 |
|
| 6523 |
RootedId id(cx, AtomToId(field)); |
| 6524 |
Rooted<mozilla::Maybe<PropertyDescriptor>> desc(cx); |
| 6525 |
RootedObject holder(cx); |
| 6526 |
if (!GetPropertyDescriptor(cx, obj, id, &desc, &holder)) { |
| 6527 |
return false; |
| 6528 |
} |
| 6529 |
|
| 6530 |
if (!desc.isSome()) { |
| 6531 |
return LinkFail(cx, "property not present on object"); |
| 6532 |
} |
| 6533 |
|
| 6534 |
if (!desc->isDataDescriptor()) { |
| 6535 |
return LinkFail(cx, "property is not a data property"); |
| 6536 |
} |
| 6537 |
|
| 6538 |
v.set(desc->value()); |
| 6539 |
return true; |
| 6540 |
} |
| 6541 |
|
| 6542 |
static bool GetDataProperty(JSContext* cx, HandleValue objVal, |
| 6543 |
const char* fieldChars, MutableHandleValue v) { |
| 6544 |
Rooted<JSAtom*> field(cx, |
| 6545 |
AtomizeUTF8Chars(cx, fieldChars, strlen(fieldChars))); |
| 6546 |
if (!field) { |
| 6547 |
return false; |
| 6548 |
} |
| 6549 |
|
| 6550 |
return GetDataProperty(cx, objVal, field, v); |
| 6551 |
} |
| 6552 |
|
| 6553 |
static bool GetDataProperty(JSContext* cx, HandleValue objVal, |
| 6554 |
const ImmutableTenuredPtr<PropertyName*>& field, |
| 6555 |
MutableHandleValue v) { |
| 6556 |
Handle<PropertyName*> fieldHandle = field; |
| 6557 |
return GetDataProperty(cx, objVal, fieldHandle, v); |
| 6558 |
} |
| 6559 |
|
| 6560 |
static bool HasObjectValueOfMethodPure(JSObject* obj, JSContext* cx) { |
| 6561 |
Value v; |
| 6562 |
if (!GetPropertyPure(cx, obj, NameToId(cx->names().valueOf), &v)) { |
| 6563 |
return false; |
| 6564 |
} |
| 6565 |
|
| 6566 |
JSFunction* fun; |
| 6567 |
if (!IsFunctionObject(v, &fun)) { |
| 6568 |
return false; |
| 6569 |
} |
| 6570 |
|
| 6571 |
return IsSelfHostedFunctionWithName(fun, cx->names().Object_valueOf); |
| 6572 |
} |
| 6573 |
|
| 6574 |
static bool HasPureCoercion(JSContext* cx, HandleValue v) { |
| 6575 |
// Ideally, we'd reject all non-primitives, but Emscripten has a bug that |
| 6576 |
// generates code that passes functions for some imports. To avoid breaking |
| 6577 |
// all the code that contains this bug, we make an exception for functions |
| 6578 |
// that don't have user-defined valueOf or toString, for their coercions |
| 6579 |
// are not observable and coercion via ToNumber/ToInt32 definitely produces |
| 6580 |
// NaN/0. We should remove this special case later once most apps have been |
| 6581 |
// built with newer Emscripten. |
| 6582 |
return v.toObject().is<JSFunction>() && |
| 6583 |
HasNoToPrimitiveMethodPure(&v.toObject(), cx) && |
| 6584 |
HasObjectValueOfMethodPure(&v.toObject(), cx) && |
| 6585 |
HasNativeMethodPure(&v.toObject(), cx->names().toString, fun_toString, |
| 6586 |
cx); |
| 6587 |
} |
| 6588 |
|
| 6589 |
static bool ValidateGlobalVariable(JSContext* cx, const AsmJSGlobal& global, |
| 6590 |
HandleValue importVal, |
| 6591 |
Maybe<LitValPOD>* val) { |
| 6592 |
switch (global.varInitKind()) { |
| 6593 |
case AsmJSGlobal::InitConstant: |
| 6594 |
val->emplace(global.varInitVal()); |
| 6595 |
return true; |
| 6596 |
|
| 6597 |
case AsmJSGlobal::InitImport: { |
| 6598 |
RootedValue v(cx); |
| 6599 |
if (!GetDataProperty(cx, importVal, global.field(), &v)) { |
| 6600 |
return false; |
| 6601 |
} |
| 6602 |
|
| 6603 |
if (!v.isPrimitive() && !HasPureCoercion(cx, v)) { |
| 6604 |
return LinkFail(cx, "Imported values must be primitives"); |
| 6605 |
} |
| 6606 |
|
| 6607 |
switch (global.varInitImportType().kind()) { |
| 6608 |
case ValType::I32: { |
| 6609 |
int32_t i32; |
| 6610 |
if (!ToInt32(cx, v, &i32)) { |
| 6611 |
return false; |
| 6612 |
} |
| 6613 |
val->emplace(uint32_t(i32)); |
| 6614 |
return true; |
| 6615 |
} |
| 6616 |
case ValType::I64: |
| 6617 |
MOZ_CRASH("int64"); |
| 6618 |
case ValType::V128: |
| 6619 |
MOZ_CRASH("v128"); |
| 6620 |
case ValType::F32: { |
| 6621 |
float f; |
| 6622 |
if (!RoundFloat32(cx, v, &f)) { |
| 6623 |
return false; |
| 6624 |
} |
| 6625 |
val->emplace(f); |
| 6626 |
return true; |
| 6627 |
} |
| 6628 |
case ValType::F64: { |
| 6629 |
double d; |
| 6630 |
if (!ToNumber(cx, v, &d)) { |
| 6631 |
return false; |
| 6632 |
} |
| 6633 |
val->emplace(d); |
| 6634 |
return true; |
| 6635 |
} |
| 6636 |
case ValType::Ref: { |
| 6637 |
MOZ_CRASH("not available in asm.js"); |
| 6638 |
} |
| 6639 |
} |
| 6640 |
} |
| 6641 |
} |
| 6642 |
|
| 6643 |
MOZ_CRASH("unreachable"); |
| 6644 |
} |
| 6645 |
|
| 6646 |
static bool ValidateFFI(JSContext* cx, const AsmJSGlobal& global, |
| 6647 |
HandleValue importVal, |
| 6648 |
MutableHandle<FunctionVector> ffis) { |
| 6649 |
RootedValue v(cx); |
| 6650 |
if (!GetDataProperty(cx, importVal, global.field(), &v)) { |
| 6651 |
return false; |
| 6652 |
} |
| 6653 |
|
| 6654 |
if (!IsFunctionObject(v)) { |
| 6655 |
return LinkFail(cx, "FFI imports must be functions"); |
| 6656 |
} |
| 6657 |
|
| 6658 |
ffis[global.ffiIndex()].set(&v.toObject().as<JSFunction>()); |
| 6659 |
return true; |
| 6660 |
} |
| 6661 |
|
| 6662 |
static bool ValidateArrayView(JSContext* cx, const AsmJSGlobal& global, |
| 6663 |
HandleValue globalVal) { |
| 6664 |
if (!global.field()) { |
| 6665 |
return true; |
| 6666 |
} |
| 6667 |
|
| 6668 |
if (Scalar::isBigIntType(global.viewType())) { |
| 6669 |
return LinkFail(cx, "bad typed array constructor"); |
| 6670 |
} |
| 6671 |
|
| 6672 |
RootedValue v(cx); |
| 6673 |
if (!GetDataProperty(cx, globalVal, global.field(), &v)) { |
| 6674 |
return false; |
| 6675 |
} |
| 6676 |
|
| 6677 |
bool tac = IsTypedArrayConstructor(v, global.viewType()); |
| 6678 |
if (!tac) { |
| 6679 |
return LinkFail(cx, "bad typed array constructor"); |
| 6680 |
} |
| 6681 |
|
| 6682 |
return true; |
| 6683 |
} |
| 6684 |
|
| 6685 |
static InlinableNative ToInlinableNative(AsmJSMathBuiltinFunction func) { |
| 6686 |
switch (func) { |
| 6687 |
case AsmJSMathBuiltin_sin: |
| 6688 |
return InlinableNative::MathSin; |
| 6689 |
case AsmJSMathBuiltin_cos: |
| 6690 |
return InlinableNative::MathCos; |
| 6691 |
case AsmJSMathBuiltin_tan: |
| 6692 |
return InlinableNative::MathTan; |
| 6693 |
case AsmJSMathBuiltin_asin: |
| 6694 |
return InlinableNative::MathASin; |
| 6695 |
case AsmJSMathBuiltin_acos: |
| 6696 |
return InlinableNative::MathACos; |
| 6697 |
case AsmJSMathBuiltin_atan: |
| 6698 |
return InlinableNative::MathATan; |
| 6699 |
case AsmJSMathBuiltin_ceil: |
| 6700 |
return InlinableNative::MathCeil; |
| 6701 |
case AsmJSMathBuiltin_floor: |
| 6702 |
return InlinableNative::MathFloor; |
| 6703 |
case AsmJSMathBuiltin_exp: |
| 6704 |
return InlinableNative::MathExp; |
| 6705 |
case AsmJSMathBuiltin_log: |
| 6706 |
return InlinableNative::MathLog; |
| 6707 |
case AsmJSMathBuiltin_pow: |
| 6708 |
return InlinableNative::MathPow; |
| 6709 |
case AsmJSMathBuiltin_sqrt: |
| 6710 |
return InlinableNative::MathSqrt; |
| 6711 |
case AsmJSMathBuiltin_abs: |
| 6712 |
return InlinableNative::MathAbs; |
| 6713 |
case AsmJSMathBuiltin_atan2: |
| 6714 |
return InlinableNative::MathATan2; |
| 6715 |
case AsmJSMathBuiltin_imul: |
| 6716 |
return InlinableNative::MathImul; |
| 6717 |
case AsmJSMathBuiltin_fround: |
| 6718 |
return InlinableNative::MathFRound; |
| 6719 |
case AsmJSMathBuiltin_min: |
| 6720 |
return InlinableNative::MathMin; |
| 6721 |
case AsmJSMathBuiltin_max: |
| 6722 |
return InlinableNative::MathMax; |
| 6723 |
case AsmJSMathBuiltin_clz32: |
| 6724 |
return InlinableNative::MathClz32; |
| 6725 |
} |
| 6726 |
MOZ_CRASH("Invalid asm.js math builtin function"); |
| 6727 |
} |
| 6728 |
|
| 6729 |
static bool ValidateMathBuiltinFunction( |
| 6730 |
JSContext* cx, const CodeMetadataForAsmJSImpl& codeMetaForAsmJS, |
| 6731 |
const AsmJSGlobal& global, HandleValue globalVal) { |
| 6732 |
RootedValue v(cx); |
| 6733 |
if (!GetDataProperty(cx, globalVal, cx->names().Math, &v)) { |
| 6734 |
return false; |
| 6735 |
} |
| 6736 |
|
| 6737 |
if (!GetDataProperty(cx, v, global.field(), &v)) { |
| 6738 |
return false; |
| 6739 |
} |
| 6740 |
|
| 6741 |
InlinableNative native = ToInlinableNative(global.mathBuiltinFunction()); |
| 6742 |
|
| 6743 |
JSFunction* fun; |
| 6744 |
if (!IsFunctionObject(v, &fun) || !fun->hasJitInfo() || |
| 6745 |
fun->jitInfo()->type() != JSJitInfo::InlinableNative || |
| 6746 |
fun->jitInfo()->inlinableNative != native) { |
| 6747 |
return LinkFail(cx, "bad Math.* builtin function"); |
| 6748 |
} |
| 6749 |
if (fun->realm()->creationOptions().alwaysUseFdlibm() != |
| 6750 |
codeMetaForAsmJS.alwaysUseFdlibm) { |
| 6751 |
return LinkFail(cx, |
| 6752 |
"Math.* builtin function and asm.js use different native" |
| 6753 |
" math implementations."); |
| 6754 |
} |
| 6755 |
|
| 6756 |
return true; |
| 6757 |
} |
| 6758 |
|
| 6759 |
static bool ValidateConstant(JSContext* cx, const AsmJSGlobal& global, |
| 6760 |
HandleValue globalVal) { |
| 6761 |
RootedValue v(cx, globalVal); |
| 6762 |
|
| 6763 |
if (global.constantKind() == AsmJSGlobal::MathConstant) { |
| 6764 |
if (!GetDataProperty(cx, v, cx->names().Math, &v)) { |
| 6765 |
return false; |
| 6766 |
} |
| 6767 |
} |
| 6768 |
|
| 6769 |
if (!GetDataProperty(cx, v, global.field(), &v)) { |
| 6770 |
return false; |
| 6771 |
} |
| 6772 |
|
| 6773 |
if (!v.isNumber()) { |
| 6774 |
return LinkFail(cx, "math / global constant value needs to be a number"); |
| 6775 |
} |
| 6776 |
|
| 6777 |
// NaN != NaN |
| 6778 |
if (std::isnan(global.constantValue())) { |
| 6779 |
if (!std::isnan(v.toNumber())) { |
| 6780 |
return LinkFail(cx, "global constant value needs to be NaN"); |
| 6781 |
} |
| 6782 |
} else { |
| 6783 |
if (v.toNumber() != global.constantValue()) { |
| 6784 |
return LinkFail(cx, "global constant value mismatch"); |
| 6785 |
} |
| 6786 |
} |
| 6787 |
|
| 6788 |
return true; |
| 6789 |
} |
| 6790 |
|
| 6791 |
static bool CheckBuffer(JSContext* cx, const CodeMetadata& codeMeta, |
| 6792 |
HandleValue bufferVal, |
| 6793 |
MutableHandle<ArrayBufferObject*> buffer) { |
| 6794 |
if (!bufferVal.isObject()) { |
| 6795 |
return LinkFail(cx, "buffer must be an object"); |
| 6796 |
} |
| 6797 |
JSObject* bufferObj = &bufferVal.toObject(); |
| 6798 |
|
| 6799 |
if (codeMeta.memories[0].isShared()) { |
| 6800 |
if (!bufferObj->is<SharedArrayBufferObject>()) { |
| 6801 |
return LinkFail( |
| 6802 |
cx, "shared views can only be constructed onto SharedArrayBuffer"); |
| 6803 |
} |
| 6804 |
return LinkFail(cx, "Unable to prepare SharedArrayBuffer for asm.js use"); |
| 6805 |
} |
| 6806 |
|
| 6807 |
if (!bufferObj->is<ArrayBufferObject>()) { |
| 6808 |
return LinkFail(cx, |
| 6809 |
"unshared views can only be constructed onto ArrayBuffer"); |
| 6810 |
} |
| 6811 |
|
| 6812 |
buffer.set(&bufferObj->as<ArrayBufferObject>()); |
| 6813 |
|
| 6814 |
size_t memoryLength = buffer->byteLength(); |
| 6815 |
|
| 6816 |
if (!IsValidAsmJSHeapLength(memoryLength)) { |
| 6817 |
UniqueChars msg; |
| 6818 |
if (memoryLength > MaxHeapLength) { |
| 6819 |
msg = JS_smprintf("ArrayBuffer byteLength 0x%" PRIx64 |
| 6820 |
" is not a valid heap length - it is too long." |
| 6821 |
" The longest valid length is 0x%" PRIx64, |
| 6822 |
uint64_t(memoryLength), MaxHeapLength); |
| 6823 |
} else { |
| 6824 |
msg = JS_smprintf("ArrayBuffer byteLength 0x%" PRIx64 |
| 6825 |
" is not a valid heap length. The next " |
| 6826 |
"valid length is 0x%" PRIx64, |
| 6827 |
uint64_t(memoryLength), |
| 6828 |
RoundUpToNextValidAsmJSHeapLength(memoryLength)); |
| 6829 |
} |
| 6830 |
if (!msg) { |
| 6831 |
return false; |
| 6832 |
} |
| 6833 |
return LinkFail(cx, msg.get()); |
| 6834 |
} |
| 6835 |
|
| 6836 |
// This check is sufficient without considering the size of the loaded datum |
| 6837 |
// because heap loads and stores start on an aligned boundary and the heap |
| 6838 |
// byteLength has larger alignment. |
| 6839 |
uint64_t minMemoryLength = codeMeta.memories.length() != 0 |
| 6840 |
? codeMeta.memories[0].initialLength32() |
| 6841 |
: 0; |
| 6842 |
MOZ_ASSERT((minMemoryLength - 1) <= INT32_MAX); |
| 6843 |
if (memoryLength < minMemoryLength) { |
| 6844 |
UniqueChars msg(JS_smprintf("ArrayBuffer byteLength of 0x%" PRIx64 |
| 6845 |
" is less than 0x%" PRIx64 " (the " |
| 6846 |
"size implied " |
| 6847 |
"by const heap accesses).", |
| 6848 |
uint64_t(memoryLength), minMemoryLength)); |
| 6849 |
if (!msg) { |
| 6850 |
return false; |
| 6851 |
} |
| 6852 |
return LinkFail(cx, msg.get()); |
| 6853 |
} |
| 6854 |
|
| 6855 |
// ArrayBuffer lengths in SpiderMonkey used to be restricted to <= INT32_MAX, |
| 6856 |
// but that has since been relaxed for the benefit of wasm. We keep the old |
| 6857 |
// limit for asm.js so as to avoid having to worry about whether the asm.js |
| 6858 |
// implementation is safe for larger heaps. |
| 6859 |
if (memoryLength >= INT32_MAX) { |
| 6860 |
UniqueChars msg( |
| 6861 |
JS_smprintf("ArrayBuffer byteLength 0x%" PRIx64 |
| 6862 |
" is too large for asm.js (implementation limit).", |
| 6863 |
uint64_t(memoryLength))); |
| 6864 |
if (!msg) { |
| 6865 |
return false; |
| 6866 |
} |
| 6867 |
return LinkFail(cx, msg.get()); |
| 6868 |
} |
| 6869 |
|
| 6870 |
if (buffer->isResizable()) { |
| 6871 |
return LinkFail(cx, |
| 6872 |
"Unable to prepare resizable ArrayBuffer for asm.js use"); |
| 6873 |
} |
| 6874 |
|
| 6875 |
if (!buffer->prepareForAsmJS()) { |
| 6876 |
return LinkFail(cx, "Unable to prepare ArrayBuffer for asm.js use"); |
| 6877 |
} |
| 6878 |
|
| 6879 |
MOZ_ASSERT(buffer->isPreparedForAsmJS()); |
| 6880 |
return true; |
| 6881 |
} |
| 6882 |
|
| 6883 |
static bool GetImports(JSContext* cx, |
| 6884 |
const CodeMetadataForAsmJSImpl& codeMetaForAsmJS, |
| 6885 |
HandleValue globalVal, HandleValue importVal, |
| 6886 |
ImportValues* imports) { |
| 6887 |
Rooted<FunctionVector> ffis(cx, FunctionVector(cx)); |
| 6888 |
if (!ffis.resize(codeMetaForAsmJS.numFFIs)) { |
| 6889 |
return false; |
| 6890 |
} |
| 6891 |
|
| 6892 |
for (const AsmJSGlobal& global : codeMetaForAsmJS.asmJSGlobals) { |
| 6893 |
switch (global.which()) { |
| 6894 |
case AsmJSGlobal::Variable: { |
| 6895 |
Maybe<LitValPOD> litVal; |
| 6896 |
if (!ValidateGlobalVariable(cx, global, importVal, &litVal)) { |
| 6897 |
return false; |
| 6898 |
} |
| 6899 |
if (!imports->globalValues.append(Val(litVal->asLitVal()))) { |
| 6900 |
return false; |
| 6901 |
} |
| 6902 |
break; |
| 6903 |
} |
| 6904 |
case AsmJSGlobal::FFI: |
| 6905 |
if (!ValidateFFI(cx, global, importVal, &ffis)) { |
| 6906 |
return false; |
| 6907 |
} |
| 6908 |
break; |
| 6909 |
case AsmJSGlobal::ArrayView: |
| 6910 |
case AsmJSGlobal::ArrayViewCtor: |
| 6911 |
if (!ValidateArrayView(cx, global, globalVal)) { |
| 6912 |
return false; |
| 6913 |
} |
| 6914 |
break; |
| 6915 |
case AsmJSGlobal::MathBuiltinFunction: |
| 6916 |
if (!ValidateMathBuiltinFunction(cx, codeMetaForAsmJS, global, |
| 6917 |
globalVal)) { |
| 6918 |
return false; |
| 6919 |
} |
| 6920 |
break; |
| 6921 |
case AsmJSGlobal::Constant: |
| 6922 |
if (!ValidateConstant(cx, global, globalVal)) { |
| 6923 |
return false; |
| 6924 |
} |
| 6925 |
break; |
| 6926 |
} |
| 6927 |
} |
| 6928 |
|
| 6929 |
for (const AsmJSImport& import : codeMetaForAsmJS.asmJSImports) { |
| 6930 |
if (!imports->funcs.append(ffis[import.ffiIndex()])) { |
| 6931 |
return false; |
| 6932 |
} |
| 6933 |
} |
| 6934 |
|
| 6935 |
return true; |
| 6936 |
} |
| 6937 |
|
| 6938 |
static bool TryInstantiate(JSContext* cx, const CallArgs& args, |
| 6939 |
const Module& module, |
| 6940 |
const CodeMetadataForAsmJSImpl& codeMetaForAsmJS, |
| 6941 |
MutableHandle<WasmInstanceObject*> instanceObj, |
| 6942 |
MutableHandleObject exportObj) { |
| 6943 |
HandleValue globalVal = args.get(0); |
| 6944 |
HandleValue importVal = args.get(1); |
| 6945 |
HandleValue bufferVal = args.get(2); |
| 6946 |
|
| 6947 |
MOZ_RELEASE_ASSERT(HasPlatformSupport()); |
| 6948 |
|
| 6949 |
if (!wasm::EnsureFullSignalHandlers(cx)) { |
| 6950 |
return LinkFail(cx, "failed to install signal handlers"); |
| 6951 |
} |
| 6952 |
|
| 6953 |
Rooted<ImportValues> imports(cx); |
| 6954 |
|
| 6955 |
if (module.codeMeta().memories.length() != 0) { |
| 6956 |
MOZ_ASSERT(module.codeMeta().memories.length() == 1); |
| 6957 |
Rooted<ArrayBufferObject*> buffer(cx); |
| 6958 |
if (!CheckBuffer(cx, module.codeMeta(), bufferVal, &buffer)) { |
| 6959 |
return false; |
| 6960 |
} |
| 6961 |
|
| 6962 |
Rooted<WasmMemoryObject*> memory( |
| 6963 |
cx, WasmMemoryObject::create(cx, buffer, /* isHuge= */ false, nullptr)); |
| 6964 |
if (!memory || !imports.get().memories.append(memory)) { |
| 6965 |
return false; |
| 6966 |
} |
| 6967 |
} |
| 6968 |
|
| 6969 |
if (!GetImports(cx, codeMetaForAsmJS, globalVal, importVal, |
| 6970 |
imports.address())) { |
| 6971 |
return false; |
| 6972 |
} |
| 6973 |
|
| 6974 |
if (!module.instantiate(cx, imports.get(), nullptr, instanceObj)) { |
| 6975 |
return false; |
| 6976 |
} |
| 6977 |
|
| 6978 |
exportObj.set(&instanceObj->exportsObj()); |
| 6979 |
return true; |
| 6980 |
} |
| 6981 |
|
| 6982 |
static bool HandleInstantiationFailure( |
| 6983 |
JSContext* cx, const CallArgs& args, |
| 6984 |
const CodeMetadataForAsmJSImpl& codeMetaForAsmJS) { |
| 6985 |
using js::frontend::FunctionSyntaxKind; |
| 6986 |
|
| 6987 |
Rooted<JSAtom*> name(cx, args.callee().as<JSFunction>().fullExplicitName()); |
| 6988 |
|
| 6989 |
if (cx->isExceptionPending()) { |
| 6990 |
return false; |
| 6991 |
} |
| 6992 |
|
| 6993 |
ScriptSource* source = codeMetaForAsmJS.maybeScriptSource(); |
| 6994 |
|
| 6995 |
// Source discarding is allowed to affect JS semantics because it is never |
| 6996 |
// enabled for normal JS content. |
| 6997 |
bool haveSource; |
| 6998 |
if (!ScriptSource::loadSource(cx, source, &haveSource)) { |
| 6999 |
return false; |
| 7000 |
} |
| 7001 |
if (!haveSource) { |
| 7002 |
JS_ReportErrorASCII(cx, |
| 7003 |
"asm.js link failure with source discarding enabled"); |
| 7004 |
return false; |
| 7005 |
} |
| 7006 |
|
| 7007 |
uint32_t begin = codeMetaForAsmJS.toStringStart; |
| 7008 |
uint32_t end = codeMetaForAsmJS.srcEndAfterCurly(); |
| 7009 |
Rooted<JSLinearString*> src(cx, source->substringDontDeflate(cx, begin, end)); |
| 7010 |
if (!src) { |
| 7011 |
return false; |
| 7012 |
} |
| 7013 |
|
| 7014 |
JS::CompileOptions options(cx); |
| 7015 |
options.setMutedErrors(source->mutedErrors()) |
| 7016 |
.setFile(source->filename()) |
| 7017 |
.setNoScriptRval(false); |
| 7018 |
options.setAsmJSOption(AsmJSOption::DisabledByLinker); |
| 7019 |
|
| 7020 |
// The exported function inherits an implicit strict context if the module |
| 7021 |
// also inherited it somehow. |
| 7022 |
if (codeMetaForAsmJS.strict) { |
| 7023 |
options.setForceStrictMode(); |
| 7024 |
} |
| 7025 |
|
| 7026 |
AutoStableStringChars linearChars(cx); |
| 7027 |
if (!linearChars.initTwoByte(cx, src)) { |
| 7028 |
return false; |
| 7029 |
} |
| 7030 |
|
| 7031 |
SourceText<char16_t> srcBuf; |
| 7032 |
if (!srcBuf.initMaybeBorrowed(cx, linearChars)) { |
| 7033 |
return false; |
| 7034 |
} |
| 7035 |
|
| 7036 |
FunctionSyntaxKind syntaxKind = FunctionSyntaxKind::Statement; |
| 7037 |
|
| 7038 |
RootedFunction fun(cx, frontend::CompileStandaloneFunction( |
| 7039 |
cx, options, srcBuf, Nothing(), syntaxKind)); |
| 7040 |
if (!fun) { |
| 7041 |
return false; |
| 7042 |
} |
| 7043 |
|
| 7044 |
fun->initEnvironment(&cx->global()->lexicalEnvironment()); |
| 7045 |
|
| 7046 |
// Call the function we just recompiled. |
| 7047 |
args.setCallee(ObjectValue(*fun)); |
| 7048 |
return InternalCallOrConstruct( |
| 7049 |
cx, args, args.isConstructing() ? CONSTRUCT : NO_CONSTRUCT); |
| 7050 |
} |
| 7051 |
|
| 7052 |
static const Module& AsmJSModuleFunctionToModule(JSFunction* fun) { |
| 7053 |
MOZ_ASSERT(IsAsmJSModule(fun)); |
| 7054 |
const Value& v = fun->getExtendedSlot(FunctionExtended::ASMJS_MODULE_SLOT); |
| 7055 |
return v.toObject().as<WasmModuleObject>().module(); |
| 7056 |
} |
| 7057 |
|
| 7058 |
// Implements the semantics of an asm.js module function that has been |
| 7059 |
// successfully validated. |
| 7060 |
bool js::InstantiateAsmJS(JSContext* cx, unsigned argc, JS::Value* vp) { |
| 7061 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 7062 |
|
| 7063 |
JSFunction* callee = &args.callee().as<JSFunction>(); |
| 7064 |
const Module& module = AsmJSModuleFunctionToModule(callee); |
| 7065 |
const CodeMetadataForAsmJSImpl& codeMetaForAsmJS = |
| 7066 |
module.codeMetaForAsmJS()->asAsmJS(); |
| 7067 |
|
| 7068 |
Rooted<WasmInstanceObject*> instanceObj(cx); |
| 7069 |
RootedObject exportObj(cx); |
| 7070 |
if (!TryInstantiate(cx, args, module, codeMetaForAsmJS, &instanceObj, |
| 7071 |
&exportObj)) { |
| 7072 |
// Link-time validation checks failed, so reparse the entire asm.js |
| 7073 |
// module from scratch to get normal interpreted bytecode which we can |
| 7074 |
// simply Invoke. Very slow. |
| 7075 |
return HandleInstantiationFailure(cx, args, codeMetaForAsmJS); |
| 7076 |
} |
| 7077 |
|
| 7078 |
args.rval().set(ObjectValue(*exportObj)); |
| 7079 |
return true; |
| 7080 |
} |
| 7081 |
|
| 7082 |
/*****************************************************************************/ |
| 7083 |
// Top-level js::CompileAsmJS |
| 7084 |
|
| 7085 |
static bool NoExceptionPending(FrontendContext* fc) { return !fc->hadErrors(); } |
| 7086 |
|
| 7087 |
static bool SuccessfulValidation(frontend::ParserBase& parser, |
| 7088 |
unsigned compilationTime) { |
| 7089 |
unsigned errNum = js::SupportDifferentialTesting() |
| 7090 |
? JSMSG_USE_ASM_TYPE_OK_NO_TIME |
| 7091 |
: JSMSG_USE_ASM_TYPE_OK; |
| 7092 |
|
| 7093 |
char timeChars[20]; |
| 7094 |
SprintfLiteral(timeChars, "%u", compilationTime); |
| 7095 |
|
| 7096 |
return parser.warningNoOffset(errNum, timeChars); |
| 7097 |
} |
| 7098 |
|
| 7099 |
static bool TypeFailureWarning(frontend::ParserBase& parser, const char* str) { |
| 7100 |
if (parser.options().throwOnAsmJSValidationFailure()) { |
| 7101 |
parser.errorNoOffset(JSMSG_USE_ASM_TYPE_FAIL, str ? str : ""); |
| 7102 |
return false; |
| 7103 |
} |
| 7104 |
|
| 7105 |
// Per the asm.js standard convention, whether failure sets a pending |
| 7106 |
// exception determines whether to attempt non-asm.js reparsing, so ignore |
| 7107 |
// the return value below. |
| 7108 |
(void)parser.warningNoOffset(JSMSG_USE_ASM_TYPE_FAIL, str ? str : ""); |
| 7109 |
return false; |
| 7110 |
} |
| 7111 |
|
| 7112 |
// asm.js requires Ion to be available on the current hardware/OS and to be |
| 7113 |
// enabled for wasm, since asm.js compilation goes via wasm. |
| 7114 |
static bool IsAsmJSCompilerAvailable(JSContext* cx) { |
| 7115 |
return HasPlatformSupport() && WasmCompilerForAsmJSAvailable(cx); |
| 7116 |
} |
| 7117 |
|
| 7118 |
static bool EstablishPreconditions(frontend::ParserBase& parser) { |
| 7119 |
switch (parser.options().asmJSOption()) { |
| 7120 |
case AsmJSOption::DisabledByAsmJSPref: |
| 7121 |
return TypeFailureWarning( |
| 7122 |
parser, "Asm.js optimizer disabled by 'asmjs' runtime option"); |
| 7123 |
case AsmJSOption::DisabledByLinker: |
| 7124 |
return TypeFailureWarning( |
| 7125 |
parser, |
| 7126 |
"Asm.js optimizer disabled by linker (instantiation failure)"); |
| 7127 |
case AsmJSOption::DisabledByNoWasmCompiler: |
| 7128 |
return TypeFailureWarning(parser, |
| 7129 |
"Asm.js optimizer disabled because no suitable " |
| 7130 |
"wasm compiler is available"); |
| 7131 |
case AsmJSOption::DisabledByDebugger: |
| 7132 |
return TypeFailureWarning( |
| 7133 |
parser, "Asm.js optimizer disabled because debugger is active"); |
| 7134 |
case AsmJSOption::Enabled: |
| 7135 |
break; |
| 7136 |
} |
| 7137 |
|
| 7138 |
if (parser.pc_->isGenerator()) { |
| 7139 |
return TypeFailureWarning(parser, |
| 7140 |
"Asm.js optimizer disabled in generator context"); |
| 7141 |
} |
| 7142 |
|
| 7143 |
if (parser.pc_->isAsync()) { |
| 7144 |
return TypeFailureWarning(parser, |
| 7145 |
"Asm.js optimizer disabled in async context"); |
| 7146 |
} |
| 7147 |
|
| 7148 |
if (parser.pc_->isArrowFunction()) { |
| 7149 |
return TypeFailureWarning( |
| 7150 |
parser, "Asm.js optimizer disabled in arrow function context"); |
| 7151 |
} |
| 7152 |
|
| 7153 |
// Class constructors are also methods |
| 7154 |
if (parser.pc_->isMethod() || parser.pc_->isGetterOrSetter()) { |
| 7155 |
return TypeFailureWarning( |
| 7156 |
parser, |
| 7157 |
"Asm.js optimizer disabled in class constructor or method context"); |
| 7158 |
} |
| 7159 |
|
| 7160 |
return true; |
| 7161 |
} |
| 7162 |
|
| 7163 |
template <typename Unit> |
| 7164 |
static bool DoCompileAsmJS(FrontendContext* fc, ParserAtomsTable& parserAtoms, |
| 7165 |
AsmJSParser<Unit>& parser, ParseNode* stmtList, |
| 7166 |
bool* validated) { |
| 7167 |
*validated = false; |
| 7168 |
|
| 7169 |
// Various conditions disable asm.js optimizations. |
| 7170 |
if (!EstablishPreconditions(parser)) { |
| 7171 |
return NoExceptionPending(fc); |
| 7172 |
} |
| 7173 |
|
| 7174 |
// "Checking" parses, validates and compiles, producing a fully compiled |
| 7175 |
// WasmModuleObject as result. |
| 7176 |
unsigned time; |
| 7177 |
SharedModule module = CheckModule(fc, parserAtoms, parser, stmtList, &time); |
| 7178 |
if (!module) { |
| 7179 |
return NoExceptionPending(fc); |
| 7180 |
} |
| 7181 |
|
| 7182 |
// Finished! Save the ref-counted module on the FunctionBox. When JSFunctions |
| 7183 |
// are eventually allocated we will create an asm.js constructor for it. |
| 7184 |
FunctionBox* funbox = parser.pc_->functionBox(); |
| 7185 |
MOZ_ASSERT(funbox->isInterpreted()); |
| 7186 |
if (!funbox->setAsmJSModule(module)) { |
| 7187 |
return NoExceptionPending(fc); |
| 7188 |
} |
| 7189 |
|
| 7190 |
// Success! Write to the console with a "warning" message indicating |
| 7191 |
// total compilation time. |
| 7192 |
*validated = true; |
| 7193 |
SuccessfulValidation(parser, time); |
| 7194 |
return NoExceptionPending(fc); |
| 7195 |
} |
| 7196 |
|
| 7197 |
bool js::CompileAsmJS(FrontendContext* fc, ParserAtomsTable& parserAtoms, |
| 7198 |
AsmJSParser<char16_t>& parser, ParseNode* stmtList, |
| 7199 |
bool* validated) { |
| 7200 |
return DoCompileAsmJS(fc, parserAtoms, parser, stmtList, validated); |
| 7201 |
} |
| 7202 |
|
| 7203 |
bool js::CompileAsmJS(FrontendContext* fc, ParserAtomsTable& parserAtoms, |
| 7204 |
AsmJSParser<Utf8Unit>& parser, ParseNode* stmtList, |
| 7205 |
bool* validated) { |
| 7206 |
return DoCompileAsmJS(fc, parserAtoms, parser, stmtList, validated); |
| 7207 |
} |
| 7208 |
|
| 7209 |
/*****************************************************************************/ |
| 7210 |
// asm.js testing functions |
| 7211 |
|
| 7212 |
bool js::IsAsmJSModuleNative(Native native) { |
| 7213 |
return native == InstantiateAsmJS; |
| 7214 |
} |
| 7215 |
|
| 7216 |
bool js::IsAsmJSModule(JSFunction* fun) { |
| 7217 |
return fun->maybeNative() == InstantiateAsmJS; |
| 7218 |
} |
| 7219 |
|
| 7220 |
bool js::IsAsmJSFunction(JSFunction* fun) { |
| 7221 |
return fun->kind() == FunctionFlags::AsmJS; |
| 7222 |
} |
| 7223 |
|
| 7224 |
bool js::IsAsmJSStrictModeModuleOrFunction(JSFunction* fun) { |
| 7225 |
if (IsAsmJSModule(fun)) { |
| 7226 |
return AsmJSModuleFunctionToModule(fun) |
| 7227 |
.codeMetaForAsmJS() |
| 7228 |
->asAsmJS() |
| 7229 |
.strict; |
| 7230 |
} |
| 7231 |
|
| 7232 |
if (IsAsmJSFunction(fun)) { |
| 7233 |
return fun->wasmInstance().codeMetaForAsmJS()->asAsmJS().strict; |
| 7234 |
} |
| 7235 |
|
| 7236 |
return false; |
| 7237 |
} |
| 7238 |
|
| 7239 |
bool js::IsAsmJSCompilationAvailable(JSContext* cx) { |
| 7240 |
return cx->options().asmJS() && IsAsmJSCompilerAvailable(cx); |
| 7241 |
} |
| 7242 |
|
| 7243 |
bool js::IsAsmJSCompilationAvailable(JSContext* cx, unsigned argc, Value* vp) { |
| 7244 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 7245 |
bool available = IsAsmJSCompilationAvailable(cx); |
| 7246 |
args.rval().set(BooleanValue(available)); |
| 7247 |
return true; |
| 7248 |
} |
| 7249 |
|
| 7250 |
static JSFunction* MaybeWrappedNativeFunction(const Value& v) { |
| 7251 |
if (!v.isObject()) { |
| 7252 |
return nullptr; |
| 7253 |
} |
| 7254 |
|
| 7255 |
return v.toObject().maybeUnwrapIf<JSFunction>(); |
| 7256 |
} |
| 7257 |
|
| 7258 |
bool js::IsAsmJSModule(JSContext* cx, unsigned argc, Value* vp) { |
| 7259 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 7260 |
|
| 7261 |
bool rval = false; |
| 7262 |
if (JSFunction* fun = MaybeWrappedNativeFunction(args.get(0))) { |
| 7263 |
rval = IsAsmJSModule(fun); |
| 7264 |
} |
| 7265 |
|
| 7266 |
args.rval().set(BooleanValue(rval)); |
| 7267 |
return true; |
| 7268 |
} |
| 7269 |
|
| 7270 |
bool js::IsAsmJSFunction(JSContext* cx, unsigned argc, Value* vp) { |
| 7271 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 7272 |
|
| 7273 |
bool rval = false; |
| 7274 |
if (JSFunction* fun = MaybeWrappedNativeFunction(args.get(0))) { |
| 7275 |
rval = IsAsmJSFunction(fun); |
| 7276 |
} |
| 7277 |
|
| 7278 |
args.rval().set(BooleanValue(rval)); |
| 7279 |
return true; |
| 7280 |
} |
| 7281 |
|
| 7282 |
/*****************************************************************************/ |
| 7283 |
// asm.js toString/toSource support |
| 7284 |
|
| 7285 |
JSString* js::AsmJSModuleToString(JSContext* cx, HandleFunction fun, |
| 7286 |
bool isToSource) { |
| 7287 |
MOZ_ASSERT(IsAsmJSModule(fun)); |
| 7288 |
|
| 7289 |
const CodeMetadataForAsmJSImpl& codeMetaForAsmJS = |
| 7290 |
AsmJSModuleFunctionToModule(fun).codeMetaForAsmJS()->asAsmJS(); |
| 7291 |
uint32_t begin = codeMetaForAsmJS.toStringStart; |
| 7292 |
uint32_t end = codeMetaForAsmJS.srcEndAfterCurly(); |
| 7293 |
ScriptSource* source = codeMetaForAsmJS.maybeScriptSource(); |
| 7294 |
|
| 7295 |
JSStringBuilder out(cx); |
| 7296 |
|
| 7297 |
if (isToSource && fun->isLambda() && !out.append("(")) { |
| 7298 |
return nullptr; |
| 7299 |
} |
| 7300 |
|
| 7301 |
bool haveSource; |
| 7302 |
if (!ScriptSource::loadSource(cx, source, &haveSource)) { |
| 7303 |
return nullptr; |
| 7304 |
} |
| 7305 |
|
| 7306 |
if (!haveSource) { |
| 7307 |
if (!out.append("function ")) { |
| 7308 |
return nullptr; |
| 7309 |
} |
| 7310 |
if (fun->fullExplicitName() && !out.append(fun->fullExplicitName())) { |
| 7311 |
return nullptr; |
| 7312 |
} |
| 7313 |
if (!out.append("() {\n [native code]\n}")) { |
| 7314 |
return nullptr; |
| 7315 |
} |
| 7316 |
} else { |
| 7317 |
Rooted<JSLinearString*> src(cx, source->substring(cx, begin, end)); |
| 7318 |
if (!src) { |
| 7319 |
return nullptr; |
| 7320 |
} |
| 7321 |
|
| 7322 |
if (!out.append(src)) { |
| 7323 |
return nullptr; |
| 7324 |
} |
| 7325 |
} |
| 7326 |
|
| 7327 |
if (isToSource && fun->isLambda() && !out.append(")")) { |
| 7328 |
return nullptr; |
| 7329 |
} |
| 7330 |
|
| 7331 |
return out.finishString(); |
| 7332 |
} |
| 7333 |
|
| 7334 |
JSString* js::AsmJSFunctionToString(JSContext* cx, HandleFunction fun) { |
| 7335 |
MOZ_ASSERT(IsAsmJSFunction(fun)); |
| 7336 |
|
| 7337 |
const CodeMetadataForAsmJSImpl& codeMetaForAsmJS = |
| 7338 |
fun->wasmInstance().codeMetaForAsmJS()->asAsmJS(); |
| 7339 |
const AsmJSExport& f = |
| 7340 |
codeMetaForAsmJS.lookupAsmJSExport(fun->wasmFuncIndex()); |
| 7341 |
|
| 7342 |
uint32_t begin = codeMetaForAsmJS.srcStart + f.startOffsetInModule(); |
| 7343 |
uint32_t end = codeMetaForAsmJS.srcStart + f.endOffsetInModule(); |
| 7344 |
|
| 7345 |
ScriptSource* source = codeMetaForAsmJS.maybeScriptSource(); |
| 7346 |
JSStringBuilder out(cx); |
| 7347 |
|
| 7348 |
if (!out.append("function ")) { |
| 7349 |
return nullptr; |
| 7350 |
} |
| 7351 |
|
| 7352 |
bool haveSource; |
| 7353 |
if (!ScriptSource::loadSource(cx, source, &haveSource)) { |
| 7354 |
return nullptr; |
| 7355 |
} |
| 7356 |
|
| 7357 |
if (!haveSource) { |
| 7358 |
// asm.js functions can't be anonymous |
| 7359 |
MOZ_ASSERT(fun->fullExplicitName()); |
| 7360 |
if (!out.append(fun->fullExplicitName())) { |
| 7361 |
return nullptr; |
| 7362 |
} |
| 7363 |
if (!out.append("() {\n [native code]\n}")) { |
| 7364 |
return nullptr; |
| 7365 |
} |
| 7366 |
} else { |
| 7367 |
Rooted<JSLinearString*> src(cx, source->substring(cx, begin, end)); |
| 7368 |
if (!src) { |
| 7369 |
return nullptr; |
| 7370 |
} |
| 7371 |
if (!out.append(src)) { |
| 7372 |
return nullptr; |
| 7373 |
} |
| 7374 |
} |
| 7375 |
|
| 7376 |
return out.finishString(); |
| 7377 |
} |
| 7378 |
|
| 7379 |
bool js::IsValidAsmJSHeapLength(size_t length) { |
| 7380 |
if (length < MinHeapLength) { |
| 7381 |
return false; |
| 7382 |
} |
| 7383 |
|
| 7384 |
// The heap length is limited by what a wasm memory32 can handle. |
| 7385 |
if (length > MaxMemoryBytes(AddressType::I32)) { |
| 7386 |
return false; |
| 7387 |
} |
| 7388 |
|
| 7389 |
return wasm::IsValidARMImmediate(length); |
| 7390 |
} |