WebAssembly: When Wasm calls to C, it should use Wasm::Context* instead of ExecState...
[WebKit-https.git] / Source / JavaScriptCore / wasm / WasmB3IRGenerator.cpp
1 /*
2  * Copyright (C) 2016-2017 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "WasmB3IRGenerator.h"
28
29 #if ENABLE(WEBASSEMBLY)
30
31 #include "AllowMacroScratchRegisterUsageIf.h"
32 #include "B3BasicBlockInlines.h"
33 #include "B3CCallValue.h"
34 #include "B3Compile.h"
35 #include "B3ConstPtrValue.h"
36 #include "B3FixSSA.h"
37 #include "B3Generate.h"
38 #include "B3InsertionSet.h"
39 #include "B3SlotBaseValue.h"
40 #include "B3StackmapGenerationParams.h"
41 #include "B3SwitchValue.h"
42 #include "B3Validate.h"
43 #include "B3ValueInlines.h"
44 #include "B3ValueKey.h"
45 #include "B3Variable.h"
46 #include "B3VariableValue.h"
47 #include "B3WasmAddressValue.h"
48 #include "B3WasmBoundsCheckValue.h"
49 #include "JSCInlines.h"
50 #include "JSWebAssemblyInstance.h"
51 #include "JSWebAssemblyModule.h"
52 #include "JSWebAssemblyRuntimeError.h"
53 #include "VirtualRegister.h"
54 #include "WasmCallingConvention.h"
55 #include "WasmContext.h"
56 #include "WasmExceptionType.h"
57 #include "WasmFunctionParser.h"
58 #include "WasmMemory.h"
59 #include "WasmOpcodeOrigin.h"
60 #include "WasmThunks.h"
61 #include <wtf/Optional.h>
62
63 void dumpProcedure(void* ptr)
64 {
65     JSC::B3::Procedure* proc = static_cast<JSC::B3::Procedure*>(ptr);
66     proc->dump(WTF::dataFile());
67 }
68
69 namespace JSC { namespace Wasm {
70
71 using namespace B3;
72
73 namespace {
74 const bool verbose = false;
75 }
76
77 class B3IRGenerator {
78 public:
79     struct ControlData {
80         ControlData(Procedure& proc, Type signature, BlockType type, BasicBlock* continuation, BasicBlock* special = nullptr)
81             : blockType(type)
82             , continuation(continuation)
83             , special(special)
84         {
85             if (signature != Void)
86                 result.append(proc.addVariable(toB3Type(signature)));
87         }
88
89         ControlData()
90         {
91         }
92
93         void dump(PrintStream& out) const
94         {
95             switch (type()) {
96             case BlockType::If:
97                 out.print("If:       ");
98                 break;
99             case BlockType::Block:
100                 out.print("Block:    ");
101                 break;
102             case BlockType::Loop:
103                 out.print("Loop:     ");
104                 break;
105             case BlockType::TopLevel:
106                 out.print("TopLevel: ");
107                 break;
108             }
109             out.print("Continuation: ", *continuation, ", Special: ");
110             if (special)
111                 out.print(*special);
112             else
113                 out.print("None");
114         }
115
116         BlockType type() const { return blockType; }
117
118         bool hasNonVoidSignature() const { return result.size(); }
119
120         BasicBlock* targetBlockForBranch()
121         {
122             if (type() == BlockType::Loop)
123                 return special;
124             return continuation;
125         }
126
127         void convertIfToBlock()
128         {
129             ASSERT(type() == BlockType::If);
130             blockType = BlockType::Block;
131             special = nullptr;
132         }
133
134     private:
135         friend class B3IRGenerator;
136         BlockType blockType;
137         BasicBlock* continuation;
138         BasicBlock* special;
139         Vector<Variable*, 1> result;
140     };
141
142     typedef Value* ExpressionType;
143     typedef ControlData ControlType;
144     typedef Vector<ExpressionType, 1> ExpressionList;
145     typedef Vector<Variable*, 1> ResultList;
146     typedef FunctionParser<B3IRGenerator>::ControlEntry ControlEntry;
147
148     static constexpr ExpressionType emptyExpression = nullptr;
149
150     typedef String ErrorType;
151     typedef UnexpectedType<ErrorType> UnexpectedResult;
152     typedef Expected<std::unique_ptr<WasmInternalFunction>, ErrorType> Result;
153     typedef Expected<void, ErrorType> PartialResult;
154     template <typename ...Args>
155     NEVER_INLINE UnexpectedResult WARN_UNUSED_RETURN fail(Args... args) const
156     {
157         using namespace FailureHelper; // See ADL comment in WasmParser.h.
158         return UnexpectedResult(makeString(ASCIILiteral("WebAssembly.Module failed compiling: "), makeString(args)...));
159     }
160 #define WASM_COMPILE_FAIL_IF(condition, ...) do { \
161         if (UNLIKELY(condition))                  \
162             return fail(__VA_ARGS__);             \
163     } while (0)
164
165     B3IRGenerator(const ModuleInformation&, Procedure&, WasmInternalFunction*, Vector<UnlinkedWasmToWasmCall>&, MemoryMode);
166
167     PartialResult WARN_UNUSED_RETURN addArguments(const Signature*);
168     PartialResult WARN_UNUSED_RETURN addLocal(Type, uint32_t);
169     ExpressionType addConstant(Type, uint64_t);
170
171     // Locals
172     PartialResult WARN_UNUSED_RETURN getLocal(uint32_t index, ExpressionType& result);
173     PartialResult WARN_UNUSED_RETURN setLocal(uint32_t index, ExpressionType value);
174
175     // Globals
176     PartialResult WARN_UNUSED_RETURN getGlobal(uint32_t index, ExpressionType& result);
177     PartialResult WARN_UNUSED_RETURN setGlobal(uint32_t index, ExpressionType value);
178
179     // Memory
180     PartialResult WARN_UNUSED_RETURN load(LoadOpType, ExpressionType pointer, ExpressionType& result, uint32_t offset);
181     PartialResult WARN_UNUSED_RETURN store(StoreOpType, ExpressionType pointer, ExpressionType value, uint32_t offset);
182     PartialResult WARN_UNUSED_RETURN addGrowMemory(ExpressionType delta, ExpressionType& result);
183     PartialResult WARN_UNUSED_RETURN addCurrentMemory(ExpressionType& result);
184
185     // Basic operators
186     template<OpType>
187     PartialResult WARN_UNUSED_RETURN addOp(ExpressionType arg, ExpressionType& result);
188     template<OpType>
189     PartialResult WARN_UNUSED_RETURN addOp(ExpressionType left, ExpressionType right, ExpressionType& result);
190     PartialResult WARN_UNUSED_RETURN addSelect(ExpressionType condition, ExpressionType nonZero, ExpressionType zero, ExpressionType& result);
191
192     // Control flow
193     ControlData WARN_UNUSED_RETURN addTopLevel(Type signature);
194     ControlData WARN_UNUSED_RETURN addBlock(Type signature);
195     ControlData WARN_UNUSED_RETURN addLoop(Type signature);
196     PartialResult WARN_UNUSED_RETURN addIf(ExpressionType condition, Type signature, ControlData& result);
197     PartialResult WARN_UNUSED_RETURN addElse(ControlData&, const ExpressionList&);
198     PartialResult WARN_UNUSED_RETURN addElseToUnreachable(ControlData&);
199
200     PartialResult WARN_UNUSED_RETURN addReturn(const ControlData&, const ExpressionList& returnValues);
201     PartialResult WARN_UNUSED_RETURN addBranch(ControlData&, ExpressionType condition, const ExpressionList& returnValues);
202     PartialResult WARN_UNUSED_RETURN addSwitch(ExpressionType condition, const Vector<ControlData*>& targets, ControlData& defaultTargets, const ExpressionList& expressionStack);
203     PartialResult WARN_UNUSED_RETURN endBlock(ControlEntry&, ExpressionList& expressionStack);
204     PartialResult WARN_UNUSED_RETURN addEndToUnreachable(ControlEntry&);
205
206     // Calls
207     PartialResult WARN_UNUSED_RETURN addCall(uint32_t calleeIndex, const Signature*, Vector<ExpressionType>& args, ExpressionType& result);
208     PartialResult WARN_UNUSED_RETURN addCallIndirect(const Signature*, SignatureIndex, Vector<ExpressionType>& args, ExpressionType& result);
209     PartialResult WARN_UNUSED_RETURN addUnreachable();
210
211     void dump(const Vector<ControlEntry>& controlStack, const ExpressionList* expressionStack);
212     void setParser(FunctionParser<B3IRGenerator>* parser) { m_parser = parser; };
213
214     Value* constant(B3::Type, uint64_t bits);
215     void insertConstants();
216
217 private:
218     void emitExceptionCheck(CCallHelpers&, ExceptionType);
219
220     ExpressionType emitCheckAndPreparePointer(ExpressionType pointer, uint32_t offset, uint32_t sizeOfOp);
221     B3::Kind memoryKind(B3::Opcode memoryOp);
222     ExpressionType emitLoadOp(LoadOpType, ExpressionType pointer, uint32_t offset);
223     void emitStoreOp(StoreOpType, ExpressionType pointer, ExpressionType value, uint32_t offset);
224
225     void unify(Variable* target, const ExpressionType source);
226     void unifyValuesWithBlock(const ExpressionList& resultStack, ResultList& stack);
227
228     void emitChecksForModOrDiv(B3::Opcode, ExpressionType left, ExpressionType right);
229
230     Value* materializeWasmContext(Procedure&, BasicBlock*);
231     void restoreWasmContext(Procedure&, BasicBlock*, Value*);
232     void restoreWebAssemblyGlobalState(const MemoryInformation&, Value* instance, Procedure&, BasicBlock*);
233
234     Origin origin();
235
236     FunctionParser<B3IRGenerator>* m_parser;
237     const ModuleInformation& m_info;
238     MemoryMode m_mode;
239     Procedure& m_proc;
240     BasicBlock* m_currentBlock;
241     Vector<Variable*> m_locals;
242     Vector<UnlinkedWasmToWasmCall>& m_unlinkedWasmToWasmCalls; // List each call site and the function index whose address it should be patched with.
243     HashMap<ValueKey, Value*> m_constantPool;
244     InsertionSet m_constantInsertionValues;
245     GPRReg m_memoryBaseGPR;
246     GPRReg m_memorySizeGPR;
247     GPRReg m_wasmContextGPR;
248     Value* m_instanceValue; // FIXME: make this lazy https://bugs.webkit.org/show_bug.cgi?id=169792
249 };
250
251 Value* B3IRGenerator::materializeWasmContext(Procedure& proc, BasicBlock* block)
252 {
253     if (useFastTLSForContext()) {
254         PatchpointValue* patchpoint = block->appendNew<PatchpointValue>(proc, pointerType(), Origin());
255         if (CCallHelpers::loadWasmContextNeedsMacroScratchRegister())
256             patchpoint->clobber(RegisterSet::macroScratchRegisters());
257         patchpoint->setGenerator(
258             [=] (CCallHelpers& jit, const StackmapGenerationParams& params) {
259                 AllowMacroScratchRegisterUsageIf allowScratch(jit, CCallHelpers::loadWasmContextNeedsMacroScratchRegister());
260                 jit.loadWasmContext(params[0].gpr());
261             });
262         return patchpoint;
263     }
264
265     // FIXME: Because WasmToWasm call clobbers wasmContext register and does not restore it, we need to restore it in the caller side.
266     // This prevents us from using ArgumentReg to this (logically) immutable pinned register.
267     PatchpointValue* patchpoint = block->appendNew<PatchpointValue>(proc, pointerType(), Origin());
268     patchpoint->effects.writesPinned = false;
269     patchpoint->effects.readsPinned = true;
270     patchpoint->resultConstraint = ValueRep::reg(m_wasmContextGPR);
271     patchpoint->setGenerator([] (CCallHelpers&, const StackmapGenerationParams&) { });
272     return patchpoint;
273 }
274
275 void B3IRGenerator::restoreWasmContext(Procedure& proc, BasicBlock* block, Value* arg)
276 {
277     if (useFastTLSForContext()) {
278         PatchpointValue* patchpoint = block->appendNew<PatchpointValue>(proc, B3::Void, Origin());
279         if (CCallHelpers::storeWasmContextNeedsMacroScratchRegister())
280             patchpoint->clobber(RegisterSet::macroScratchRegisters());
281         patchpoint->append(ConstrainedValue(arg, ValueRep::SomeRegister));
282         patchpoint->setGenerator(
283             [=] (CCallHelpers& jit, const StackmapGenerationParams& params) {
284                 AllowMacroScratchRegisterUsageIf allowScratch(jit, CCallHelpers::storeWasmContextNeedsMacroScratchRegister());
285                 jit.storeWasmContext(params[0].gpr());
286             });
287         return;
288     }
289
290     // FIXME: Because WasmToWasm call clobbers wasmContext register and does not restore it, we need to restore it in the caller side.
291     // This prevents us from using ArgumentReg to this (logically) immutable pinned register.
292     PatchpointValue* patchpoint = block->appendNew<PatchpointValue>(proc, B3::Void, Origin());
293     Effects effects = Effects::none();
294     effects.writesPinned = true;
295     effects.reads = B3::HeapRange::top();
296     patchpoint->effects = effects;
297     patchpoint->clobberLate(RegisterSet(m_wasmContextGPR));
298     patchpoint->append(m_instanceValue, ValueRep::SomeRegister);
299     GPRReg wasmContextGPR = m_wasmContextGPR;
300     patchpoint->setGenerator([=] (CCallHelpers& jit, const StackmapGenerationParams& param) {
301         jit.move(param[0].gpr(), wasmContextGPR);
302     });
303 }
304
305 B3IRGenerator::B3IRGenerator(const ModuleInformation& info, Procedure& procedure, WasmInternalFunction* compilation, Vector<UnlinkedWasmToWasmCall>& unlinkedWasmToWasmCalls, MemoryMode mode)
306     : m_info(info)
307     , m_mode(mode)
308     , m_proc(procedure)
309     , m_unlinkedWasmToWasmCalls(unlinkedWasmToWasmCalls)
310     , m_constantInsertionValues(m_proc)
311 {
312     m_currentBlock = m_proc.addBlock();
313
314     // FIXME we don't really need to pin registers here if there's no memory. It makes wasm -> wasm thunks simpler for now. https://bugs.webkit.org/show_bug.cgi?id=166623
315     const PinnedRegisterInfo& pinnedRegs = PinnedRegisterInfo::get();
316     m_memoryBaseGPR = pinnedRegs.baseMemoryPointer;
317     m_wasmContextGPR = pinnedRegs.wasmContextPointer;
318     m_proc.pinRegister(m_memoryBaseGPR);
319     if (!useFastTLSForContext())
320         m_proc.pinRegister(m_wasmContextGPR);
321     ASSERT(!pinnedRegs.sizeRegisters[0].sizeOffset);
322     m_memorySizeGPR = pinnedRegs.sizeRegisters[0].sizeRegister;
323     for (const PinnedSizeRegisterInfo& regInfo : pinnedRegs.sizeRegisters)
324         m_proc.pinRegister(regInfo.sizeRegister);
325
326     if (info.memory) {
327         m_proc.setWasmBoundsCheckGenerator([=] (CCallHelpers& jit, GPRReg pinnedGPR, unsigned) {
328             AllowMacroScratchRegisterUsage allowScratch(jit);
329             ASSERT_UNUSED(pinnedGPR, m_memorySizeGPR == pinnedGPR);
330             this->emitExceptionCheck(jit, ExceptionType::OutOfBoundsMemoryAccess);
331         });
332     }
333
334     wasmCallingConvention().setupFrameInPrologue(&compilation->wasmCalleeMoveLocation, m_proc, Origin(), m_currentBlock);
335
336     m_instanceValue = materializeWasmContext(m_proc, m_currentBlock);
337 }
338
339 void B3IRGenerator::restoreWebAssemblyGlobalState(const MemoryInformation& memory, Value* instance, Procedure& proc, BasicBlock* block)
340 {
341     restoreWasmContext(proc, block, instance);
342
343     if (!!memory) {
344         const PinnedRegisterInfo* pinnedRegs = &PinnedRegisterInfo::get();
345         RegisterSet clobbers;
346         clobbers.set(pinnedRegs->baseMemoryPointer);
347         for (auto info : pinnedRegs->sizeRegisters)
348             clobbers.set(info.sizeRegister);
349
350         B3::PatchpointValue* patchpoint = block->appendNew<B3::PatchpointValue>(proc, B3::Void, origin());
351         Effects effects = Effects::none();
352         effects.writesPinned = true;
353         effects.reads = B3::HeapRange::top();
354         patchpoint->effects = effects;
355         patchpoint->clobber(clobbers);
356
357         patchpoint->append(instance, ValueRep::SomeRegister);
358
359         patchpoint->setGenerator([pinnedRegs] (CCallHelpers& jit, const B3::StackmapGenerationParams& params) {
360             GPRReg baseMemory = pinnedRegs->baseMemoryPointer;
361             jit.loadPtr(CCallHelpers::Address(params[0].gpr(), JSWebAssemblyInstance::offsetOfMemory()), baseMemory);
362             const auto& sizeRegs = pinnedRegs->sizeRegisters;
363             ASSERT(sizeRegs.size() >= 1);
364             ASSERT(!sizeRegs[0].sizeOffset); // The following code assumes we start at 0, and calculates subsequent size registers relative to 0.
365             jit.loadPtr(CCallHelpers::Address(baseMemory, JSWebAssemblyMemory::offsetOfSize()), sizeRegs[0].sizeRegister);
366             jit.loadPtr(CCallHelpers::Address(baseMemory, JSWebAssemblyMemory::offsetOfMemory()), baseMemory);
367             for (unsigned i = 1; i < sizeRegs.size(); ++i)
368                 jit.add64(CCallHelpers::TrustedImm32(-sizeRegs[i].sizeOffset), sizeRegs[0].sizeRegister, sizeRegs[i].sizeRegister);
369         });
370     }
371 }
372
373 void B3IRGenerator::emitExceptionCheck(CCallHelpers& jit, ExceptionType type)
374 {
375     jit.move(CCallHelpers::TrustedImm32(static_cast<uint32_t>(type)), GPRInfo::argumentGPR1);
376     auto jumpToExceptionStub = jit.jump();
377
378     jit.addLinkTask([jumpToExceptionStub] (LinkBuffer& linkBuffer) {
379         linkBuffer.link(jumpToExceptionStub, CodeLocationLabel(Thunks::singleton().stub(throwExceptionFromWasmThunkGenerator).code()));
380     });
381 }
382
383 Value* B3IRGenerator::constant(B3::Type type, uint64_t bits)
384 {
385     auto result = m_constantPool.ensure(ValueKey(opcodeForConstant(type), type, static_cast<int64_t>(bits)), [&] {
386         Value* result = m_proc.addConstant(origin(), type, bits);
387         m_constantInsertionValues.insertValue(0, result);
388         return result;
389     });
390     return result.iterator->value;
391 }
392
393 void B3IRGenerator::insertConstants()
394 {
395     m_constantInsertionValues.execute(m_proc.at(0));
396 }
397
398 auto B3IRGenerator::addLocal(Type type, uint32_t count) -> PartialResult
399 {
400     WASM_COMPILE_FAIL_IF(!m_locals.tryReserveCapacity(m_locals.size() + count), "can't allocate memory for ", m_locals.size() + count, " locals");
401
402     for (uint32_t i = 0; i < count; ++i) {
403         Variable* local = m_proc.addVariable(toB3Type(type));
404         m_locals.uncheckedAppend(local);
405         m_currentBlock->appendNew<VariableValue>(m_proc, Set, origin(), local, addConstant(type, 0));
406     }
407     return { };
408 }
409
410 auto B3IRGenerator::addArguments(const Signature* signature) -> PartialResult
411 {
412     ASSERT(!m_locals.size());
413     WASM_COMPILE_FAIL_IF(!m_locals.tryReserveCapacity(signature->argumentCount()), "can't allocate memory for ", signature->argumentCount(), " arguments");
414
415     m_locals.grow(signature->argumentCount());
416     wasmCallingConvention().loadArguments(signature, m_proc, m_currentBlock, Origin(),
417         [=] (ExpressionType argument, unsigned i) {
418             Variable* argumentVariable = m_proc.addVariable(argument->type());
419             m_locals[i] = argumentVariable;
420             m_currentBlock->appendNew<VariableValue>(m_proc, Set, Origin(), argumentVariable, argument);
421         });
422     return { };
423 }
424
425 auto B3IRGenerator::getLocal(uint32_t index, ExpressionType& result) -> PartialResult
426 {
427     ASSERT(m_locals[index]);
428     result = m_currentBlock->appendNew<VariableValue>(m_proc, B3::Get, origin(), m_locals[index]);
429     return { };
430 }
431
432 auto B3IRGenerator::addUnreachable() -> PartialResult
433 {
434     B3::PatchpointValue* unreachable = m_currentBlock->appendNew<B3::PatchpointValue>(m_proc, B3::Void, origin());
435     unreachable->setGenerator([this] (CCallHelpers& jit, const B3::StackmapGenerationParams&) {
436         this->emitExceptionCheck(jit, ExceptionType::Unreachable);
437     });
438     unreachable->effects.terminal = true;
439     return { };
440 }
441
442 auto B3IRGenerator::addGrowMemory(ExpressionType delta, ExpressionType& result) -> PartialResult
443 {
444     int32_t (*growMemory) (Context*, int32_t) = [] (Context* wasmContext, int32_t delta) -> int32_t {
445         VM& vm = *wasmContext->vm();
446         auto scope = DECLARE_THROW_SCOPE(vm);
447
448         JSWebAssemblyMemory* wasmMemory = wasmContext->memory();
449
450         if (delta < 0)
451             return -1;
452
453         bool shouldThrowExceptionsOnFailure = false;
454         // grow() does not require ExecState* if it doesn't throw exceptions.
455         ExecState* exec = nullptr; 
456         PageCount result = wasmMemory->grow(vm, exec, static_cast<uint32_t>(delta), shouldThrowExceptionsOnFailure);
457         RELEASE_ASSERT(!scope.exception());
458         if (!result)
459             return -1;
460
461         return result.pageCount();
462     };
463
464     result = m_currentBlock->appendNew<CCallValue>(m_proc, Int32, origin(),
465         m_currentBlock->appendNew<ConstPtrValue>(m_proc, origin(), bitwise_cast<void*>(growMemory)),
466         m_instanceValue, delta);
467
468     restoreWebAssemblyGlobalState(m_info.memory, m_instanceValue, m_proc, m_currentBlock);
469
470     return { };
471 }
472
473 auto B3IRGenerator::addCurrentMemory(ExpressionType& result) -> PartialResult
474 {
475     Value* memoryObject = m_currentBlock->appendNew<MemoryValue>(m_proc, Load, pointerType(), origin(), m_instanceValue, JSWebAssemblyInstance::offsetOfMemory());
476
477     static_assert(sizeof(decltype(static_cast<JSWebAssemblyInstance*>(nullptr)->memory()->memory().size())) == sizeof(uint64_t), "codegen relies on this size");
478     Value* size = m_currentBlock->appendNew<MemoryValue>(m_proc, Load, Int64, origin(), memoryObject, JSWebAssemblyMemory::offsetOfSize());
479     
480     constexpr uint32_t shiftValue = 16;
481     static_assert(PageCount::pageSize == 1 << shiftValue, "This must hold for the code below to be correct.");
482     Value* numPages = m_currentBlock->appendNew<Value>(m_proc, ZShr, origin(),
483         size, m_currentBlock->appendNew<Const32Value>(m_proc, origin(), shiftValue));
484
485     result = m_currentBlock->appendNew<Value>(m_proc, Trunc, origin(), numPages);
486
487     return { };
488 }
489
490 auto B3IRGenerator::setLocal(uint32_t index, ExpressionType value) -> PartialResult
491 {
492     ASSERT(m_locals[index]);
493     m_currentBlock->appendNew<VariableValue>(m_proc, B3::Set, origin(), m_locals[index], value);
494     return { };
495 }
496
497 auto B3IRGenerator::getGlobal(uint32_t index, ExpressionType& result) -> PartialResult
498 {
499     Value* globalsArray = m_currentBlock->appendNew<MemoryValue>(m_proc, Load, pointerType(), origin(), m_instanceValue, JSWebAssemblyInstance::offsetOfGlobals());
500     result = m_currentBlock->appendNew<MemoryValue>(m_proc, Load, toB3Type(m_info.globals[index].type), origin(), globalsArray, index * sizeof(Register));
501     return { };
502 }
503
504 auto B3IRGenerator::setGlobal(uint32_t index, ExpressionType value) -> PartialResult
505 {
506     ASSERT(toB3Type(m_info.globals[index].type) == value->type());
507     Value* globalsArray = m_currentBlock->appendNew<MemoryValue>(m_proc, Load, pointerType(), origin(), m_instanceValue, JSWebAssemblyInstance::offsetOfGlobals());
508     m_currentBlock->appendNew<MemoryValue>(m_proc, Store, origin(), value, globalsArray, index * sizeof(Register));
509     return { };
510 }
511
512 inline Value* B3IRGenerator::emitCheckAndPreparePointer(ExpressionType pointer, uint32_t offset, uint32_t sizeOfOperation)
513 {
514     ASSERT(m_memoryBaseGPR);
515     if (m_mode == MemoryMode::BoundsChecking) {
516         ASSERT(m_memorySizeGPR);
517         ASSERT(sizeOfOperation + offset > offset);
518         m_currentBlock->appendNew<WasmBoundsCheckValue>(m_proc, origin(), pointer, m_memorySizeGPR, sizeOfOperation + offset - 1);
519     }
520     pointer = m_currentBlock->appendNew<Value>(m_proc, ZExt32, origin(), pointer);
521     return m_currentBlock->appendNew<WasmAddressValue>(m_proc, origin(), pointer, m_memoryBaseGPR);
522 }
523
524 inline uint32_t sizeOfLoadOp(LoadOpType op)
525 {
526     switch (op) {
527     case LoadOpType::I32Load8S:
528     case LoadOpType::I32Load8U:
529     case LoadOpType::I64Load8S:
530     case LoadOpType::I64Load8U:
531         return 1;
532     case LoadOpType::I32Load16S:
533     case LoadOpType::I64Load16S:
534     case LoadOpType::I32Load16U:
535     case LoadOpType::I64Load16U:
536         return 2;
537     case LoadOpType::I32Load:
538     case LoadOpType::I64Load32S:
539     case LoadOpType::I64Load32U:
540     case LoadOpType::F32Load:
541         return 4;
542     case LoadOpType::I64Load:
543     case LoadOpType::F64Load:
544         return 8;
545     }
546     RELEASE_ASSERT_NOT_REACHED();
547 }
548
549 inline B3::Kind B3IRGenerator::memoryKind(B3::Opcode memoryOp)
550 {
551     if (m_mode == MemoryMode::Signaling)
552         return trapping(memoryOp);
553     return memoryOp;
554 }
555
556 inline Value* B3IRGenerator::emitLoadOp(LoadOpType op, ExpressionType pointer, uint32_t offset)
557 {
558     switch (op) {
559     case LoadOpType::I32Load8S: {
560         return m_currentBlock->appendNew<MemoryValue>(m_proc, memoryKind(Load8S), origin(), pointer, offset);
561     }
562
563     case LoadOpType::I64Load8S: {
564         Value* value = m_currentBlock->appendNew<MemoryValue>(m_proc, memoryKind(Load8S), origin(), pointer, offset);
565         return m_currentBlock->appendNew<Value>(m_proc, SExt32, origin(), value);
566     }
567
568     case LoadOpType::I32Load8U: {
569         return m_currentBlock->appendNew<MemoryValue>(m_proc, memoryKind(Load8Z), origin(), pointer, offset);
570     }
571
572     case LoadOpType::I64Load8U: {
573         Value* value = m_currentBlock->appendNew<MemoryValue>(m_proc, memoryKind(Load8Z), origin(), pointer, offset);
574         return m_currentBlock->appendNew<Value>(m_proc, ZExt32, origin(), value);
575     }
576
577     case LoadOpType::I32Load16S: {
578         return m_currentBlock->appendNew<MemoryValue>(m_proc, memoryKind(Load16S), origin(), pointer, offset);
579     }
580     case LoadOpType::I64Load16S: {
581         Value* value = m_currentBlock->appendNew<MemoryValue>(m_proc, memoryKind(Load16S), origin(), pointer, offset);
582         return m_currentBlock->appendNew<Value>(m_proc, SExt32, origin(), value);
583     }
584
585     case LoadOpType::I32Load: {
586         return m_currentBlock->appendNew<MemoryValue>(m_proc, memoryKind(Load), Int32, origin(), pointer, offset);
587     }
588
589     case LoadOpType::I64Load32U: {
590         Value* value = m_currentBlock->appendNew<MemoryValue>(m_proc, memoryKind(Load), Int32, origin(), pointer, offset);
591         return m_currentBlock->appendNew<Value>(m_proc, ZExt32, origin(), value);
592     }
593
594     case LoadOpType::I64Load32S: {
595         Value* value = m_currentBlock->appendNew<MemoryValue>(m_proc, memoryKind(Load), Int32, origin(), pointer, offset);
596         return m_currentBlock->appendNew<Value>(m_proc, SExt32, origin(), value);
597     }
598
599     case LoadOpType::I64Load: {
600         return m_currentBlock->appendNew<MemoryValue>(m_proc, memoryKind(Load), Int64, origin(), pointer, offset);
601     }
602
603     case LoadOpType::F32Load: {
604         return m_currentBlock->appendNew<MemoryValue>(m_proc, memoryKind(Load), Float, origin(), pointer, offset);
605     }
606
607     case LoadOpType::F64Load: {
608         return m_currentBlock->appendNew<MemoryValue>(m_proc, memoryKind(Load), Double, origin(), pointer, offset);
609     }
610
611     // FIXME: B3 doesn't support Load16Z yet. We should lower to that value when
612     // it's added. https://bugs.webkit.org/show_bug.cgi?id=165884
613     case LoadOpType::I32Load16U: {
614         Value* value = m_currentBlock->appendNew<MemoryValue>(m_proc, memoryKind(Load16S), origin(), pointer, offset);
615         return m_currentBlock->appendNew<Value>(m_proc, BitAnd, origin(), value,
616             m_currentBlock->appendNew<Const32Value>(m_proc, origin(), 0x0000ffff));
617     }
618     case LoadOpType::I64Load16U: {
619         Value* value = m_currentBlock->appendNew<MemoryValue>(m_proc, memoryKind(Load16S), origin(), pointer, offset);
620         Value* partialResult = m_currentBlock->appendNew<Value>(m_proc, BitAnd, origin(), value,
621             m_currentBlock->appendNew<Const32Value>(m_proc, origin(), 0x0000ffff));
622
623         return m_currentBlock->appendNew<Value>(m_proc, ZExt32, origin(), partialResult);
624     }
625     }
626     RELEASE_ASSERT_NOT_REACHED();
627 }
628
629 auto B3IRGenerator::load(LoadOpType op, ExpressionType pointer, ExpressionType& result, uint32_t offset) -> PartialResult
630 {
631     ASSERT(pointer->type() == Int32);
632
633     if (UNLIKELY(sumOverflows<uint32_t>(offset, sizeOfLoadOp(op)))) {
634         // FIXME: Even though this is provably out of bounds, it's not a validation error, so we have to handle it
635         // as a runtime exception. However, this may change: https://bugs.webkit.org/show_bug.cgi?id=166435
636         B3::PatchpointValue* throwException = m_currentBlock->appendNew<B3::PatchpointValue>(m_proc, B3::Void, origin());
637         throwException->setGenerator([this] (CCallHelpers& jit, const B3::StackmapGenerationParams&) {
638             this->emitExceptionCheck(jit, ExceptionType::OutOfBoundsMemoryAccess);
639         });
640
641         switch (op) {
642         case LoadOpType::I32Load8S:
643         case LoadOpType::I32Load16S:
644         case LoadOpType::I32Load:
645         case LoadOpType::I32Load16U:
646         case LoadOpType::I32Load8U:
647             result = constant(Int32, 0);
648             break;
649         case LoadOpType::I64Load8S:
650         case LoadOpType::I64Load8U:
651         case LoadOpType::I64Load16S:
652         case LoadOpType::I64Load32U:
653         case LoadOpType::I64Load32S:
654         case LoadOpType::I64Load:
655         case LoadOpType::I64Load16U:
656             result = constant(Int64, 0);
657             break;
658         case LoadOpType::F32Load:
659             result = constant(Float, 0);
660             break;
661         case LoadOpType::F64Load:
662             result = constant(Double, 0);
663             break;
664         }
665
666     } else
667         result = emitLoadOp(op, emitCheckAndPreparePointer(pointer, offset, sizeOfLoadOp(op)), offset);
668
669     return { };
670 }
671
672 inline uint32_t sizeOfStoreOp(StoreOpType op)
673 {
674     switch (op) {
675     case StoreOpType::I32Store8:
676     case StoreOpType::I64Store8:
677         return 1;
678     case StoreOpType::I32Store16:
679     case StoreOpType::I64Store16:
680         return 2;
681     case StoreOpType::I32Store:
682     case StoreOpType::I64Store32:
683     case StoreOpType::F32Store:
684         return 4;
685     case StoreOpType::I64Store:
686     case StoreOpType::F64Store:
687         return 8;
688     }
689     RELEASE_ASSERT_NOT_REACHED();
690 }
691
692
693 inline void B3IRGenerator::emitStoreOp(StoreOpType op, ExpressionType pointer, ExpressionType value, uint32_t offset)
694 {
695     switch (op) {
696     case StoreOpType::I64Store8:
697         value = m_currentBlock->appendNew<Value>(m_proc, Trunc, origin(), value);
698         FALLTHROUGH;
699
700     case StoreOpType::I32Store8:
701         m_currentBlock->appendNew<MemoryValue>(m_proc, memoryKind(Store8), origin(), value, pointer, offset);
702         return;
703
704     case StoreOpType::I64Store16:
705         value = m_currentBlock->appendNew<Value>(m_proc, Trunc, origin(), value);
706         FALLTHROUGH;
707
708     case StoreOpType::I32Store16:
709         m_currentBlock->appendNew<MemoryValue>(m_proc, memoryKind(Store16), origin(), value, pointer, offset);
710         return;
711
712     case StoreOpType::I64Store32:
713         value = m_currentBlock->appendNew<Value>(m_proc, Trunc, origin(), value);
714         FALLTHROUGH;
715
716     case StoreOpType::I64Store:
717     case StoreOpType::I32Store:
718     case StoreOpType::F32Store:
719     case StoreOpType::F64Store:
720         m_currentBlock->appendNew<MemoryValue>(m_proc, memoryKind(Store), origin(), value, pointer, offset);
721         return;
722     }
723     RELEASE_ASSERT_NOT_REACHED();
724 }
725
726 auto B3IRGenerator::store(StoreOpType op, ExpressionType pointer, ExpressionType value, uint32_t offset) -> PartialResult
727 {
728     ASSERT(pointer->type() == Int32);
729
730     if (UNLIKELY(sumOverflows<uint32_t>(offset, sizeOfStoreOp(op)))) {
731         // FIXME: Even though this is provably out of bounds, it's not a validation error, so we have to handle it
732         // as a runtime exception. However, this may change: https://bugs.webkit.org/show_bug.cgi?id=166435
733         B3::PatchpointValue* throwException = m_currentBlock->appendNew<B3::PatchpointValue>(m_proc, B3::Void, origin());
734         throwException->setGenerator([this] (CCallHelpers& jit, const B3::StackmapGenerationParams&) {
735             this->emitExceptionCheck(jit, ExceptionType::OutOfBoundsMemoryAccess);
736         });
737     } else
738         emitStoreOp(op, emitCheckAndPreparePointer(pointer, offset, sizeOfStoreOp(op)), value, offset);
739
740     return { };
741 }
742
743 auto B3IRGenerator::addSelect(ExpressionType condition, ExpressionType nonZero, ExpressionType zero, ExpressionType& result) -> PartialResult
744 {
745     result = m_currentBlock->appendNew<Value>(m_proc, B3::Select, origin(), condition, nonZero, zero);
746     return { };
747 }
748
749 B3IRGenerator::ExpressionType B3IRGenerator::addConstant(Type type, uint64_t value)
750 {
751     return constant(toB3Type(type), value);
752 }
753
754 B3IRGenerator::ControlData B3IRGenerator::addTopLevel(Type signature)
755 {
756     return ControlData(m_proc, signature, BlockType::TopLevel, m_proc.addBlock());
757 }
758
759 B3IRGenerator::ControlData B3IRGenerator::addBlock(Type signature)
760 {
761     return ControlData(m_proc, signature, BlockType::Block, m_proc.addBlock());
762 }
763
764 B3IRGenerator::ControlData B3IRGenerator::addLoop(Type signature)
765 {
766     BasicBlock* body = m_proc.addBlock();
767     BasicBlock* continuation = m_proc.addBlock();
768     m_currentBlock->appendNewControlValue(m_proc, Jump, origin(), body);
769     body->addPredecessor(m_currentBlock);
770     m_currentBlock = body;
771     return ControlData(m_proc, signature, BlockType::Loop, continuation, body);
772 }
773
774 auto B3IRGenerator::addIf(ExpressionType condition, Type signature, ControlType& result) -> PartialResult
775 {
776     // FIXME: This needs to do some kind of stack passing.
777
778     BasicBlock* taken = m_proc.addBlock();
779     BasicBlock* notTaken = m_proc.addBlock();
780     BasicBlock* continuation = m_proc.addBlock();
781
782     m_currentBlock->appendNew<Value>(m_proc, B3::Branch, origin(), condition);
783     m_currentBlock->setSuccessors(FrequentedBlock(taken), FrequentedBlock(notTaken));
784     taken->addPredecessor(m_currentBlock);
785     notTaken->addPredecessor(m_currentBlock);
786
787     m_currentBlock = taken;
788     result = ControlData(m_proc, signature, BlockType::If, continuation, notTaken);
789     return { };
790 }
791
792 auto B3IRGenerator::addElse(ControlData& data, const ExpressionList& currentStack) -> PartialResult
793 {
794     unifyValuesWithBlock(currentStack, data.result);
795     m_currentBlock->appendNewControlValue(m_proc, Jump, origin(), data.continuation);
796     return addElseToUnreachable(data);
797 }
798
799 auto B3IRGenerator::addElseToUnreachable(ControlData& data) -> PartialResult
800 {
801     ASSERT(data.type() == BlockType::If);
802     m_currentBlock = data.special;
803     data.convertIfToBlock();
804     return { };
805 }
806
807 auto B3IRGenerator::addReturn(const ControlData&, const ExpressionList& returnValues) -> PartialResult
808 {
809     ASSERT(returnValues.size() <= 1);
810     if (returnValues.size())
811         m_currentBlock->appendNewControlValue(m_proc, B3::Return, origin(), returnValues[0]);
812     else
813         m_currentBlock->appendNewControlValue(m_proc, B3::Return, origin());
814     return { };
815 }
816
817 auto B3IRGenerator::addBranch(ControlData& data, ExpressionType condition, const ExpressionList& returnValues) -> PartialResult
818 {
819     if (data.type() != BlockType::Loop)
820         unifyValuesWithBlock(returnValues, data.result);
821
822     BasicBlock* target = data.targetBlockForBranch();
823     if (condition) {
824         BasicBlock* continuation = m_proc.addBlock();
825         m_currentBlock->appendNew<Value>(m_proc, B3::Branch, origin(), condition);
826         m_currentBlock->setSuccessors(FrequentedBlock(target), FrequentedBlock(continuation));
827         target->addPredecessor(m_currentBlock);
828         continuation->addPredecessor(m_currentBlock);
829         m_currentBlock = continuation;
830     } else {
831         m_currentBlock->appendNewControlValue(m_proc, Jump, origin(), FrequentedBlock(target));
832         target->addPredecessor(m_currentBlock);
833     }
834
835     return { };
836 }
837
838 auto B3IRGenerator::addSwitch(ExpressionType condition, const Vector<ControlData*>& targets, ControlData& defaultTarget, const ExpressionList& expressionStack) -> PartialResult
839 {
840     for (size_t i = 0; i < targets.size(); ++i)
841         unifyValuesWithBlock(expressionStack, targets[i]->result);
842     unifyValuesWithBlock(expressionStack, defaultTarget.result);
843
844     SwitchValue* switchValue = m_currentBlock->appendNew<SwitchValue>(m_proc, origin(), condition);
845     switchValue->setFallThrough(FrequentedBlock(defaultTarget.targetBlockForBranch()));
846     for (size_t i = 0; i < targets.size(); ++i)
847         switchValue->appendCase(SwitchCase(i, FrequentedBlock(targets[i]->targetBlockForBranch())));
848
849     return { };
850 }
851
852 auto B3IRGenerator::endBlock(ControlEntry& entry, ExpressionList& expressionStack) -> PartialResult
853 {
854     ControlData& data = entry.controlData;
855
856     unifyValuesWithBlock(expressionStack, data.result);
857     m_currentBlock->appendNewControlValue(m_proc, Jump, origin(), data.continuation);
858     data.continuation->addPredecessor(m_currentBlock);
859
860     return addEndToUnreachable(entry);
861 }
862
863
864 auto B3IRGenerator::addEndToUnreachable(ControlEntry& entry) -> PartialResult
865 {
866     ControlData& data = entry.controlData;
867     m_currentBlock = data.continuation;
868
869     if (data.type() == BlockType::If) {
870         data.special->appendNewControlValue(m_proc, Jump, origin(), m_currentBlock);
871         m_currentBlock->addPredecessor(data.special);
872     }
873
874     for (Variable* result : data.result)
875         entry.enclosedExpressionStack.append(m_currentBlock->appendNew<VariableValue>(m_proc, B3::Get, origin(), result));
876
877     // TopLevel does not have any code after this so we need to make sure we emit a return here.
878     if (data.type() == BlockType::TopLevel)
879         return addReturn(entry.controlData, entry.enclosedExpressionStack);
880
881     return { };
882 }
883
884 auto B3IRGenerator::addCall(uint32_t functionIndex, const Signature* signature, Vector<ExpressionType>& args, ExpressionType& result) -> PartialResult
885 {
886     ASSERT(signature->argumentCount() == args.size());
887
888     Type returnType = signature->returnType();
889     Vector<UnlinkedWasmToWasmCall>* unlinkedWasmToWasmCalls = &m_unlinkedWasmToWasmCalls;
890
891     if (m_info.isImportedFunctionFromFunctionIndexSpace(functionIndex)) {
892         // FIXME imports can be linked here, instead of generating a patchpoint, because all import stubs are generated before B3 compilation starts. https://bugs.webkit.org/show_bug.cgi?id=166462
893         Value* functionImport = m_currentBlock->appendNew<MemoryValue>(m_proc, Load, pointerType(), origin(), m_instanceValue, JSWebAssemblyInstance::offsetOfImportFunction(functionIndex));
894         Value* jsTypeOfImport = m_currentBlock->appendNew<MemoryValue>(m_proc, Load8Z, origin(), functionImport, JSCell::typeInfoTypeOffset());
895         Value* isWasmCall = m_currentBlock->appendNew<Value>(m_proc, Equal, origin(), jsTypeOfImport, m_currentBlock->appendNew<Const32Value>(m_proc, origin(), WebAssemblyFunctionType));
896
897         BasicBlock* isWasmBlock = m_proc.addBlock();
898         BasicBlock* isJSBlock = m_proc.addBlock();
899         BasicBlock* continuation = m_proc.addBlock();
900         m_currentBlock->appendNewControlValue(m_proc, B3::Branch, origin(), isWasmCall, FrequentedBlock(isWasmBlock), FrequentedBlock(isJSBlock));
901
902         Value* wasmCallResult = wasmCallingConvention().setupCall(m_proc, isWasmBlock, origin(), args, toB3Type(returnType),
903             [=] (PatchpointValue* patchpoint) {
904                 patchpoint->effects.writesPinned = true;
905                 patchpoint->effects.readsPinned = true;
906                 patchpoint->clobberLate(PinnedRegisterInfo::get().toSave());
907                 patchpoint->setGenerator([unlinkedWasmToWasmCalls, functionIndex] (CCallHelpers& jit, const B3::StackmapGenerationParams&) {
908                     AllowMacroScratchRegisterUsage allowScratch(jit);
909                     CCallHelpers::Call call = jit.call();
910                     jit.addLinkTask([unlinkedWasmToWasmCalls, call, functionIndex] (LinkBuffer& linkBuffer) {
911                         unlinkedWasmToWasmCalls->append({ linkBuffer.locationOf(call), functionIndex, UnlinkedWasmToWasmCall::Target::ToWasm });
912                     });
913                 });
914             });
915         UpsilonValue* wasmCallResultUpsilon = returnType == Void ? nullptr : isWasmBlock->appendNew<UpsilonValue>(m_proc, origin(), wasmCallResult);
916         isWasmBlock->appendNewControlValue(m_proc, Jump, origin(), continuation);
917
918         Value* jsCallResult = wasmCallingConvention().setupCall(m_proc, isJSBlock, origin(), args, toB3Type(returnType),
919             [=] (PatchpointValue* patchpoint) {
920                 patchpoint->effects.writesPinned = true;
921                 patchpoint->effects.readsPinned = true;
922                 patchpoint->clobberLate(PinnedRegisterInfo::get().toSave());
923                 patchpoint->setGenerator([unlinkedWasmToWasmCalls, functionIndex] (CCallHelpers& jit, const B3::StackmapGenerationParams&) {
924                     AllowMacroScratchRegisterUsage allowScratch(jit);
925                     CCallHelpers::Call call = jit.call();
926                     jit.addLinkTask([unlinkedWasmToWasmCalls, call, functionIndex] (LinkBuffer& linkBuffer) {
927                         unlinkedWasmToWasmCalls->append({ linkBuffer.locationOf(call), functionIndex, UnlinkedWasmToWasmCall::Target::ToJs });
928                     });
929                 });
930             });
931         UpsilonValue* jsCallResultUpsilon = returnType == Void ? nullptr : isJSBlock->appendNew<UpsilonValue>(m_proc, origin(), jsCallResult);
932         isJSBlock->appendNewControlValue(m_proc, Jump, origin(), continuation);
933
934         m_currentBlock = continuation;
935
936         if (returnType == Void)
937             result = nullptr;
938         else {
939             result = continuation->appendNew<Value>(m_proc, Phi, toB3Type(returnType), origin());
940             wasmCallResultUpsilon->setPhi(result);
941             jsCallResultUpsilon->setPhi(result);
942         }
943
944         // The call could have been to another WebAssembly instance, and / or could have modified our Memory.
945         restoreWebAssemblyGlobalState(m_info.memory, m_instanceValue, m_proc, continuation);
946     } else {
947         result = wasmCallingConvention().setupCall(m_proc, m_currentBlock, origin(), args, toB3Type(returnType),
948             [=] (PatchpointValue* patchpoint) {
949                 patchpoint->effects.writesPinned = true;
950                 patchpoint->effects.readsPinned = true;
951
952                 patchpoint->setGenerator([unlinkedWasmToWasmCalls, functionIndex] (CCallHelpers& jit, const B3::StackmapGenerationParams&) {
953                     AllowMacroScratchRegisterUsage allowScratch(jit);
954                     CCallHelpers::Call call = jit.call();
955                     jit.addLinkTask([unlinkedWasmToWasmCalls, call, functionIndex] (LinkBuffer& linkBuffer) {
956                         unlinkedWasmToWasmCalls->append({ linkBuffer.locationOf(call), functionIndex, UnlinkedWasmToWasmCall::Target::ToWasm });
957                     });
958                 });
959             });
960     }
961
962     return { };
963 }
964
965 auto B3IRGenerator::addCallIndirect(const Signature* signature, SignatureIndex signatureIndex, Vector<ExpressionType>& args, ExpressionType& result) -> PartialResult
966 {
967     ASSERT(signatureIndex != Signature::invalidIndex);
968     ExpressionType calleeIndex = args.takeLast();
969     ASSERT(signature->argumentCount() == args.size());
970
971     ExpressionType callableFunctionBuffer;
972     ExpressionType callableFunctionBufferSize;
973     {
974         ExpressionType table = m_currentBlock->appendNew<MemoryValue>(m_proc, Load, pointerType(), origin(),
975             m_instanceValue, JSWebAssemblyInstance::offsetOfTable());
976         callableFunctionBuffer = m_currentBlock->appendNew<MemoryValue>(m_proc, Load, pointerType(), origin(),
977             table, JSWebAssemblyTable::offsetOfFunctions());
978         callableFunctionBufferSize = m_currentBlock->appendNew<MemoryValue>(m_proc, Load, Int32, origin(),
979             table, JSWebAssemblyTable::offsetOfSize());
980     }
981
982     // Check the index we are looking for is valid.
983     {
984         CheckValue* check = m_currentBlock->appendNew<CheckValue>(m_proc, Check, origin(),
985             m_currentBlock->appendNew<Value>(m_proc, AboveEqual, origin(), calleeIndex, callableFunctionBufferSize));
986
987         check->setGenerator([=] (CCallHelpers& jit, const B3::StackmapGenerationParams&) {
988             this->emitExceptionCheck(jit, ExceptionType::OutOfBoundsCallIndirect);
989         });
990     }
991
992     // Compute the offset in the table index space we are looking for.
993     ExpressionType offset = m_currentBlock->appendNew<Value>(m_proc, Mul, origin(),
994         m_currentBlock->appendNew<Value>(m_proc, ZExt32, origin(), calleeIndex),
995         constant(pointerType(), sizeof(CallableFunction)));
996     ExpressionType callableFunction = m_currentBlock->appendNew<Value>(m_proc, Add, origin(), callableFunctionBuffer, offset);
997
998     // Check that the CallableFunction is initialized. We trap if it isn't. An "invalid" SignatureIndex indicates it's not initialized.
999     static_assert(sizeof(CallableFunction::signatureIndex) == sizeof(uint32_t), "Load codegen assumes i32");
1000     ExpressionType calleeSignatureIndex = m_currentBlock->appendNew<MemoryValue>(m_proc, Load, Int32, origin(), callableFunction, OBJECT_OFFSETOF(CallableFunction, signatureIndex));
1001     {
1002         CheckValue* check = m_currentBlock->appendNew<CheckValue>(m_proc, Check, origin(),
1003             m_currentBlock->appendNew<Value>(m_proc, Equal, origin(),
1004                 calleeSignatureIndex,
1005                 m_currentBlock->appendNew<Const32Value>(m_proc, origin(), Signature::invalidIndex)));
1006
1007         check->setGenerator([=] (CCallHelpers& jit, const B3::StackmapGenerationParams&) {
1008             this->emitExceptionCheck(jit, ExceptionType::NullTableEntry);
1009         });
1010     }
1011
1012     // Check the signature matches the value we expect.
1013     {
1014         ExpressionType expectedSignatureIndex = m_currentBlock->appendNew<Const32Value>(m_proc, origin(), signatureIndex);
1015         CheckValue* check = m_currentBlock->appendNew<CheckValue>(m_proc, Check, origin(),
1016             m_currentBlock->appendNew<Value>(m_proc, NotEqual, origin(), calleeSignatureIndex, expectedSignatureIndex));
1017
1018         check->setGenerator([=] (CCallHelpers& jit, const B3::StackmapGenerationParams&) {
1019             this->emitExceptionCheck(jit, ExceptionType::BadSignature);
1020         });
1021     }
1022
1023     ExpressionType calleeCode = m_currentBlock->appendNew<MemoryValue>(m_proc, Load, pointerType(), origin(), callableFunction, OBJECT_OFFSETOF(CallableFunction, code));
1024
1025     Type returnType = signature->returnType();
1026     result = wasmCallingConvention().setupCall(m_proc, m_currentBlock, origin(), args, toB3Type(returnType),
1027         [=] (PatchpointValue* patchpoint) {
1028             patchpoint->effects.writesPinned = true;
1029             patchpoint->effects.readsPinned = true;
1030             patchpoint->clobberLate(PinnedRegisterInfo::get().toSave());
1031
1032             patchpoint->append(calleeCode, ValueRep::SomeRegister);
1033             patchpoint->setGenerator([=] (CCallHelpers& jit, const B3::StackmapGenerationParams& params) {
1034                 AllowMacroScratchRegisterUsage allowScratch(jit);
1035                 jit.call(params[returnType == Void ? 0 : 1].gpr());
1036             });
1037         });
1038
1039     // The call could have been to another WebAssembly instance, and / or could have modified our Memory.
1040     restoreWebAssemblyGlobalState(m_info.memory, m_instanceValue, m_proc, m_currentBlock);
1041
1042     return { };
1043 }
1044
1045 void B3IRGenerator::unify(Variable* variable, ExpressionType source)
1046 {
1047     m_currentBlock->appendNew<VariableValue>(m_proc, Set, origin(), variable, source);
1048 }
1049
1050 void B3IRGenerator::unifyValuesWithBlock(const ExpressionList& resultStack, ResultList& result)
1051 {
1052     ASSERT(result.size() <= resultStack.size());
1053
1054     for (size_t i = 0; i < result.size(); ++i)
1055         unify(result[result.size() - 1 - i], resultStack[resultStack.size() - 1 - i]);
1056 }
1057
1058 static void dumpExpressionStack(const CommaPrinter& comma, const B3IRGenerator::ExpressionList& expressionStack)
1059 {
1060     dataLog(comma, "ExpressionStack:");
1061     for (const auto& expression : expressionStack)
1062         dataLog(comma, *expression);
1063 }
1064
1065 void B3IRGenerator::dump(const Vector<ControlEntry>& controlStack, const ExpressionList* expressionStack)
1066 {
1067     dataLogLn("Constants:");
1068     for (const auto& constant : m_constantPool)
1069         dataLogLn(deepDump(m_proc, constant.value));
1070
1071     dataLogLn("Processing Graph:");
1072     dataLog(m_proc);
1073     dataLogLn("With current block:", *m_currentBlock);
1074     dataLogLn("Control stack:");
1075     ASSERT(controlStack.size());
1076     for (size_t i = controlStack.size(); i--;) {
1077         dataLog("  ", controlStack[i].controlData, ": ");
1078         CommaPrinter comma(", ", "");
1079         dumpExpressionStack(comma, *expressionStack);
1080         expressionStack = &controlStack[i].enclosedExpressionStack;
1081         dataLogLn();
1082     }
1083     dataLogLn();
1084 }
1085
1086 static void createJSToWasmWrapper(CompilationContext& compilationContext, WasmInternalFunction& function, const Signature* signature, const ModuleInformation& info)
1087 {
1088     CCallHelpers& jit = *compilationContext.jsEntrypointJIT;
1089
1090     jit.emitFunctionPrologue();
1091
1092     // FIXME Stop using 0 as codeBlocks. https://bugs.webkit.org/show_bug.cgi?id=165321
1093     jit.store64(CCallHelpers::TrustedImm64(0), CCallHelpers::Address(GPRInfo::callFrameRegister, CallFrameSlot::codeBlock * static_cast<int>(sizeof(Register))));
1094     MacroAssembler::DataLabelPtr calleeMoveLocation = jit.moveWithPatch(MacroAssembler::TrustedImmPtr(nullptr), GPRInfo::nonPreservedNonReturnGPR);
1095     jit.storePtr(GPRInfo::nonPreservedNonReturnGPR, CCallHelpers::Address(GPRInfo::callFrameRegister, CallFrameSlot::callee * static_cast<int>(sizeof(Register))));
1096     CodeLocationDataLabelPtr* linkedCalleeMove = &function.jsToWasmCalleeMoveLocation;
1097     jit.addLinkTask([=] (LinkBuffer& linkBuffer) {
1098         *linkedCalleeMove = linkBuffer.locationOf(calleeMoveLocation);
1099     });
1100
1101     const PinnedRegisterInfo& pinnedRegs = PinnedRegisterInfo::get();
1102     RegisterSet toSave = pinnedRegs.toSave();
1103
1104 #if !ASSERT_DISABLED
1105     unsigned toSaveSize = toSave.numberOfSetGPRs();
1106     // They should all be callee saves.
1107     toSave.filter(RegisterSet::calleeSaveRegisters());
1108     ASSERT(toSave.numberOfSetGPRs() == toSaveSize);
1109 #endif
1110
1111     RegisterAtOffsetList registersToSpill(toSave, RegisterAtOffsetList::OffsetBaseType::FramePointerBased);
1112     function.jsToWasmEntrypoint.calleeSaveRegisters = registersToSpill;
1113
1114     unsigned totalFrameSize = registersToSpill.size() * sizeof(void*);
1115     totalFrameSize += WasmCallingConvention::headerSizeInBytes();
1116     totalFrameSize -= sizeof(CallerFrameAndPC);
1117     unsigned numGPRs = 0;
1118     unsigned numFPRs = 0;
1119     for (unsigned i = 0; i < signature->argumentCount(); i++) {
1120         switch (signature->argument(i)) {
1121         case Wasm::I64:
1122         case Wasm::I32:
1123             if (numGPRs >= wasmCallingConvention().m_gprArgs.size())
1124                 totalFrameSize += sizeof(void*);
1125             ++numGPRs;
1126             break;
1127         case Wasm::F32:
1128         case Wasm::F64:
1129             if (numFPRs >= wasmCallingConvention().m_fprArgs.size())
1130                 totalFrameSize += sizeof(void*);
1131             ++numFPRs;
1132             break;
1133         default:
1134             RELEASE_ASSERT_NOT_REACHED();
1135         }
1136     }
1137
1138     totalFrameSize = WTF::roundUpToMultipleOf(stackAlignmentBytes(), totalFrameSize);
1139     jit.subPtr(MacroAssembler::TrustedImm32(totalFrameSize), MacroAssembler::stackPointerRegister);
1140
1141     // We save all these registers regardless of having a memory or not.
1142     // The reason is that we use one of these as a scratch. That said,
1143     // almost all real wasm programs use memory, so it's not really
1144     // worth optimizing for the case that they don't.
1145     for (const RegisterAtOffset& regAtOffset : registersToSpill) {
1146         GPRReg reg = regAtOffset.reg().gpr();
1147         ptrdiff_t offset = regAtOffset.offset();
1148         jit.storePtr(reg, CCallHelpers::Address(GPRInfo::callFrameRegister, offset));
1149     }
1150
1151     GPRReg wasmContextGPR = pinnedRegs.wasmContextPointer;
1152
1153     {
1154         CCallHelpers::Address calleeFrame = CCallHelpers::Address(MacroAssembler::stackPointerRegister, -static_cast<ptrdiff_t>(sizeof(CallerFrameAndPC)));
1155         numGPRs = 0;
1156         numFPRs = 0;
1157         // We're going to set the pinned registers after this. So
1158         // we can use this as a scratch for now since we saved it above.
1159         GPRReg scratchReg = pinnedRegs.baseMemoryPointer;
1160
1161         ptrdiff_t jsOffset = CallFrameSlot::thisArgument * sizeof(EncodedJSValue);
1162
1163         // vmEntryToWasm passes Wasm::Context* as the first JS argument when we're
1164         // not using fast TLS to hold the Wasm::Context*.
1165         if (!useFastTLSForContext()) {
1166             jit.loadPtr(CCallHelpers::Address(GPRInfo::callFrameRegister, jsOffset), wasmContextGPR);
1167             jsOffset += sizeof(EncodedJSValue);
1168         }
1169
1170         ptrdiff_t wasmOffset = CallFrame::headerSizeInRegisters * sizeof(void*);
1171         for (unsigned i = 0; i < signature->argumentCount(); i++) {
1172             switch (signature->argument(i)) {
1173             case Wasm::I32:
1174             case Wasm::I64:
1175                 if (numGPRs >= wasmCallingConvention().m_gprArgs.size()) {
1176                     if (signature->argument(i) == Wasm::I32) {
1177                         jit.load32(CCallHelpers::Address(GPRInfo::callFrameRegister, jsOffset), scratchReg);
1178                         jit.store32(scratchReg, calleeFrame.withOffset(wasmOffset));
1179                     } else {
1180                         jit.load64(CCallHelpers::Address(GPRInfo::callFrameRegister, jsOffset), scratchReg);
1181                         jit.store64(scratchReg, calleeFrame.withOffset(wasmOffset));
1182                     }
1183                     wasmOffset += sizeof(void*);
1184                 } else {
1185                     if (signature->argument(i) == Wasm::I32)
1186                         jit.load32(CCallHelpers::Address(GPRInfo::callFrameRegister, jsOffset), wasmCallingConvention().m_gprArgs[numGPRs].gpr());
1187                     else
1188                         jit.load64(CCallHelpers::Address(GPRInfo::callFrameRegister, jsOffset), wasmCallingConvention().m_gprArgs[numGPRs].gpr());
1189                 }
1190                 ++numGPRs;
1191                 break;
1192             case Wasm::F32:
1193             case Wasm::F64:
1194                 if (numFPRs >= wasmCallingConvention().m_fprArgs.size()) {
1195                     if (signature->argument(i) == Wasm::F32) {
1196                         jit.load32(CCallHelpers::Address(GPRInfo::callFrameRegister, jsOffset), scratchReg);
1197                         jit.store32(scratchReg, calleeFrame.withOffset(wasmOffset));
1198                     } else {
1199                         jit.load64(CCallHelpers::Address(GPRInfo::callFrameRegister, jsOffset), scratchReg);
1200                         jit.store64(scratchReg, calleeFrame.withOffset(wasmOffset));
1201                     }
1202                     wasmOffset += sizeof(void*);
1203                 } else {
1204                     if (signature->argument(i) == Wasm::F32)
1205                         jit.loadFloat(CCallHelpers::Address(GPRInfo::callFrameRegister, jsOffset), wasmCallingConvention().m_fprArgs[numFPRs].fpr());
1206                     else
1207                         jit.loadDouble(CCallHelpers::Address(GPRInfo::callFrameRegister, jsOffset), wasmCallingConvention().m_fprArgs[numFPRs].fpr());
1208                 }
1209                 ++numFPRs;
1210                 break;
1211             default:
1212                 RELEASE_ASSERT_NOT_REACHED();
1213             }
1214
1215             jsOffset += sizeof(EncodedJSValue);
1216         }
1217     }
1218
1219     if (!!info.memory) {
1220         GPRReg baseMemory = pinnedRegs.baseMemoryPointer;
1221
1222         if (!useFastTLSForContext())
1223             jit.loadPtr(CCallHelpers::Address(wasmContextGPR, JSWebAssemblyInstance::offsetOfMemory()), baseMemory);
1224         else {
1225             jit.loadWasmContext(baseMemory);
1226             jit.loadPtr(CCallHelpers::Address(baseMemory, JSWebAssemblyInstance::offsetOfMemory()), baseMemory);
1227         }
1228         const auto& sizeRegs = pinnedRegs.sizeRegisters;
1229         ASSERT(sizeRegs.size() >= 1);
1230         ASSERT(!sizeRegs[0].sizeOffset); // The following code assumes we start at 0, and calculates subsequent size registers relative to 0.
1231         jit.loadPtr(CCallHelpers::Address(baseMemory, JSWebAssemblyMemory::offsetOfSize()), sizeRegs[0].sizeRegister);
1232         jit.loadPtr(CCallHelpers::Address(baseMemory, JSWebAssemblyMemory::offsetOfMemory()), baseMemory);
1233         for (unsigned i = 1; i < sizeRegs.size(); ++i)
1234             jit.add64(CCallHelpers::TrustedImm32(-sizeRegs[i].sizeOffset), sizeRegs[0].sizeRegister, sizeRegs[i].sizeRegister);
1235     }
1236
1237     compilationContext.jsEntrypointToWasmEntrypointCall = jit.call();
1238
1239     for (const RegisterAtOffset& regAtOffset : registersToSpill) {
1240         GPRReg reg = regAtOffset.reg().gpr();
1241         ASSERT(reg != GPRInfo::returnValueGPR);
1242         ptrdiff_t offset = regAtOffset.offset();
1243         jit.loadPtr(CCallHelpers::Address(GPRInfo::callFrameRegister, offset), reg);
1244     }
1245
1246     switch (signature->returnType()) {
1247     case Wasm::F32:
1248         jit.moveFloatTo32(FPRInfo::returnValueFPR, GPRInfo::returnValueGPR);
1249         break;
1250     case Wasm::F64:
1251         jit.moveDoubleTo64(FPRInfo::returnValueFPR, GPRInfo::returnValueGPR);
1252         break;
1253     default:
1254         break;
1255     }
1256
1257     jit.emitFunctionEpilogue();
1258     jit.ret();
1259 }
1260
1261 auto B3IRGenerator::origin() -> Origin
1262 {
1263     return bitwise_cast<Origin>(OpcodeOrigin(m_parser->currentOpcode(), m_parser->currentOpcodeStartingOffset()));
1264 }
1265
1266 Expected<std::unique_ptr<WasmInternalFunction>, String> parseAndCompile(VM& vm, CompilationContext& compilationContext, const uint8_t* functionStart, size_t functionLength, const Signature* signature, Vector<UnlinkedWasmToWasmCall>& unlinkedWasmToWasmCalls, const ModuleInformation& info, const Vector<SignatureIndex>& moduleSignatureIndicesToUniquedSignatureIndices, MemoryMode mode, unsigned optLevel)
1267 {
1268     auto result = std::make_unique<WasmInternalFunction>();
1269
1270     compilationContext.jsEntrypointJIT = std::make_unique<CCallHelpers>();
1271     compilationContext.wasmEntrypointJIT = std::make_unique<CCallHelpers>();
1272
1273     Procedure procedure;
1274
1275     procedure.setOriginPrinter([] (PrintStream& out, Origin origin) {
1276         if (origin.data())
1277             out.print("Wasm: ", bitwise_cast<OpcodeOrigin>(origin));
1278     });
1279
1280     B3IRGenerator context(info, procedure, result.get(), unlinkedWasmToWasmCalls, mode);
1281     FunctionParser<B3IRGenerator> parser(&vm, context, functionStart, functionLength, signature, info, moduleSignatureIndicesToUniquedSignatureIndices);
1282     WASM_FAIL_IF_HELPER_FAILS(parser.parse());
1283
1284     context.insertConstants();
1285
1286     procedure.resetReachability();
1287     if (!ASSERT_DISABLED)
1288         validate(procedure, "After parsing:\n");
1289
1290     dataLogIf(verbose, "Pre SSA: ", procedure);
1291     fixSSA(procedure);
1292     dataLogIf(verbose, "Post SSA: ", procedure);
1293
1294     {
1295         B3::prepareForGeneration(procedure, optLevel);
1296         B3::generate(procedure, *compilationContext.wasmEntrypointJIT);
1297         compilationContext.wasmEntrypointByproducts = procedure.releaseByproducts();
1298         result->wasmEntrypoint.calleeSaveRegisters = procedure.calleeSaveRegisters();
1299     }
1300
1301     createJSToWasmWrapper(compilationContext, *result, signature, info);
1302     return WTFMove(result);
1303 }
1304
1305 // Custom wasm ops. These are the ones too messy to do in wasm.json.
1306
1307 void B3IRGenerator::emitChecksForModOrDiv(B3::Opcode operation, ExpressionType left, ExpressionType right)
1308 {
1309     ASSERT(operation == Div || operation == Mod || operation == UDiv || operation == UMod);
1310     const B3::Type type = left->type();
1311
1312     {
1313         CheckValue* check = m_currentBlock->appendNew<CheckValue>(m_proc, Check, origin(),
1314             m_currentBlock->appendNew<Value>(m_proc, Equal, origin(), right, constant(type, 0)));
1315
1316         check->setGenerator([=] (CCallHelpers& jit, const B3::StackmapGenerationParams&) {
1317             this->emitExceptionCheck(jit, ExceptionType::DivisionByZero);
1318         });
1319     }
1320
1321     if (operation == Div) {
1322         int64_t min = type == Int32 ? std::numeric_limits<int32_t>::min() : std::numeric_limits<int64_t>::min();
1323
1324         CheckValue* check = m_currentBlock->appendNew<CheckValue>(m_proc, Check, origin(),
1325             m_currentBlock->appendNew<Value>(m_proc, BitAnd, origin(),
1326                 m_currentBlock->appendNew<Value>(m_proc, Equal, origin(), left, constant(type, min)),
1327                 m_currentBlock->appendNew<Value>(m_proc, Equal, origin(), right, constant(type, -1))));
1328
1329         check->setGenerator([=] (CCallHelpers& jit, const B3::StackmapGenerationParams&) {
1330             this->emitExceptionCheck(jit, ExceptionType::IntegerOverflow);
1331         });
1332     }
1333 }
1334
1335 template<>
1336 auto B3IRGenerator::addOp<OpType::I32DivS>(ExpressionType left, ExpressionType right, ExpressionType& result) -> PartialResult
1337 {
1338     const B3::Opcode op = Div;
1339     emitChecksForModOrDiv(op, left, right);
1340     result = m_currentBlock->appendNew<Value>(m_proc, op, origin(), left, right);
1341     return { };
1342 }
1343
1344 template<>
1345 auto B3IRGenerator::addOp<OpType::I32RemS>(ExpressionType left, ExpressionType right, ExpressionType& result) -> PartialResult
1346 {
1347     const B3::Opcode op = Mod;
1348     emitChecksForModOrDiv(op, left, right);
1349     result = m_currentBlock->appendNew<Value>(m_proc, chill(op), origin(), left, right);
1350     return { };
1351 }
1352
1353 template<>
1354 auto B3IRGenerator::addOp<OpType::I32DivU>(ExpressionType left, ExpressionType right, ExpressionType& result) -> PartialResult
1355 {
1356     const B3::Opcode op = UDiv;
1357     emitChecksForModOrDiv(op, left, right);
1358     result = m_currentBlock->appendNew<Value>(m_proc, op, origin(), left, right);
1359     return { };
1360 }
1361
1362 template<>
1363 auto B3IRGenerator::addOp<OpType::I32RemU>(ExpressionType left, ExpressionType right, ExpressionType& result) -> PartialResult
1364 {
1365     const B3::Opcode op = UMod;
1366     emitChecksForModOrDiv(op, left, right);
1367     result = m_currentBlock->appendNew<Value>(m_proc, op, origin(), left, right);
1368     return { };
1369 }
1370
1371 template<>
1372 auto B3IRGenerator::addOp<OpType::I64DivS>(ExpressionType left, ExpressionType right, ExpressionType& result) -> PartialResult
1373 {
1374     const B3::Opcode op = Div;
1375     emitChecksForModOrDiv(op, left, right);
1376     result = m_currentBlock->appendNew<Value>(m_proc, op, origin(), left, right);
1377     return { };
1378 }
1379
1380 template<>
1381 auto B3IRGenerator::addOp<OpType::I64RemS>(ExpressionType left, ExpressionType right, ExpressionType& result) -> PartialResult
1382 {
1383     const B3::Opcode op = Mod;
1384     emitChecksForModOrDiv(op, left, right);
1385     result = m_currentBlock->appendNew<Value>(m_proc, chill(op), origin(), left, right);
1386     return { };
1387 }
1388
1389 template<>
1390 auto B3IRGenerator::addOp<OpType::I64DivU>(ExpressionType left, ExpressionType right, ExpressionType& result) -> PartialResult
1391 {
1392     const B3::Opcode op = UDiv;
1393     emitChecksForModOrDiv(op, left, right);
1394     result = m_currentBlock->appendNew<Value>(m_proc, op, origin(), left, right);
1395     return { };
1396 }
1397
1398 template<>
1399 auto B3IRGenerator::addOp<OpType::I64RemU>(ExpressionType left, ExpressionType right, ExpressionType& result) -> PartialResult
1400 {
1401     const B3::Opcode op = UMod;
1402     emitChecksForModOrDiv(op, left, right);
1403     result = m_currentBlock->appendNew<Value>(m_proc, op, origin(), left, right);
1404     return { };
1405 }
1406
1407 template<>
1408 auto B3IRGenerator::addOp<OpType::I32Ctz>(ExpressionType arg, ExpressionType& result) -> PartialResult
1409 {
1410     PatchpointValue* patchpoint = m_currentBlock->appendNew<PatchpointValue>(m_proc, Int32, origin());
1411     patchpoint->append(arg, ValueRep::SomeRegister);
1412     patchpoint->setGenerator([=] (CCallHelpers& jit, const StackmapGenerationParams& params) {
1413         jit.countTrailingZeros32(params[1].gpr(), params[0].gpr());
1414     });
1415     patchpoint->effects = Effects::none();
1416     result = patchpoint;
1417     return { };
1418 }
1419
1420 template<>
1421 auto B3IRGenerator::addOp<OpType::I64Ctz>(ExpressionType arg, ExpressionType& result) -> PartialResult
1422 {
1423     PatchpointValue* patchpoint = m_currentBlock->appendNew<PatchpointValue>(m_proc, Int64, origin());
1424     patchpoint->append(arg, ValueRep::SomeRegister);
1425     patchpoint->setGenerator([=] (CCallHelpers& jit, const StackmapGenerationParams& params) {
1426         jit.countTrailingZeros64(params[1].gpr(), params[0].gpr());
1427     });
1428     patchpoint->effects = Effects::none();
1429     result = patchpoint;
1430     return { };
1431 }
1432
1433 template<>
1434 auto B3IRGenerator::addOp<OpType::I32Popcnt>(ExpressionType arg, ExpressionType& result) -> PartialResult
1435 {
1436     // FIXME: This should use the popcnt instruction if SSE4 is available but we don't have code to detect SSE4 yet.
1437     // see: https://bugs.webkit.org/show_bug.cgi?id=165363
1438     uint32_t (*popcount)(int32_t) = [] (int32_t value) -> uint32_t { return __builtin_popcount(value); };
1439     Value* funcAddress = m_currentBlock->appendNew<ConstPtrValue>(m_proc, origin(), bitwise_cast<void*>(popcount));
1440     result = m_currentBlock->appendNew<CCallValue>(m_proc, Int32, origin(), Effects::none(), funcAddress, arg);
1441     return { };
1442 }
1443
1444 template<>
1445 auto B3IRGenerator::addOp<OpType::I64Popcnt>(ExpressionType arg, ExpressionType& result) -> PartialResult
1446 {
1447     // FIXME: This should use the popcnt instruction if SSE4 is available but we don't have code to detect SSE4 yet.
1448     // see: https://bugs.webkit.org/show_bug.cgi?id=165363
1449     uint64_t (*popcount)(int64_t) = [] (int64_t value) -> uint64_t { return __builtin_popcountll(value); };
1450     Value* funcAddress = m_currentBlock->appendNew<ConstPtrValue>(m_proc, origin(), bitwise_cast<void*>(popcount));
1451     result = m_currentBlock->appendNew<CCallValue>(m_proc, Int64, origin(), Effects::none(), funcAddress, arg);
1452     return { };
1453 }
1454
1455 template<>
1456 auto B3IRGenerator::addOp<F64ConvertUI64>(ExpressionType arg, ExpressionType& result) -> PartialResult
1457 {
1458     PatchpointValue* patchpoint = m_currentBlock->appendNew<PatchpointValue>(m_proc, Double, origin());
1459     if (isX86())
1460         patchpoint->numGPScratchRegisters = 1;
1461     patchpoint->append(ConstrainedValue(arg, ValueRep::SomeRegister));
1462     patchpoint->setGenerator([=] (CCallHelpers& jit, const StackmapGenerationParams& params) {
1463         AllowMacroScratchRegisterUsage allowScratch(jit);
1464 #if CPU(X86_64)
1465         jit.convertUInt64ToDouble(params[1].gpr(), params[0].fpr(), params.gpScratch(0));
1466 #else
1467         jit.convertUInt64ToDouble(params[1].gpr(), params[0].fpr());
1468 #endif
1469     });
1470     patchpoint->effects = Effects::none();
1471     result = patchpoint;
1472     return { };
1473 }
1474
1475 template<>
1476 auto B3IRGenerator::addOp<OpType::F32ConvertUI64>(ExpressionType arg, ExpressionType& result) -> PartialResult
1477 {
1478     PatchpointValue* patchpoint = m_currentBlock->appendNew<PatchpointValue>(m_proc, Float, origin());
1479     if (isX86())
1480         patchpoint->numGPScratchRegisters = 1;
1481     patchpoint->append(ConstrainedValue(arg, ValueRep::SomeRegister));
1482     patchpoint->setGenerator([=] (CCallHelpers& jit, const StackmapGenerationParams& params) {
1483         AllowMacroScratchRegisterUsage allowScratch(jit);
1484 #if CPU(X86_64)
1485         jit.convertUInt64ToFloat(params[1].gpr(), params[0].fpr(), params.gpScratch(0));
1486 #else
1487         jit.convertUInt64ToFloat(params[1].gpr(), params[0].fpr());
1488 #endif
1489     });
1490     patchpoint->effects = Effects::none();
1491     result = patchpoint;
1492     return { };
1493 }
1494
1495 template<>
1496 auto B3IRGenerator::addOp<OpType::F64Nearest>(ExpressionType arg, ExpressionType& result) -> PartialResult
1497 {
1498     PatchpointValue* patchpoint = m_currentBlock->appendNew<PatchpointValue>(m_proc, Double, origin());
1499     patchpoint->append(arg, ValueRep::SomeRegister);
1500     patchpoint->setGenerator([=] (CCallHelpers& jit, const StackmapGenerationParams& params) {
1501         jit.roundTowardNearestIntDouble(params[1].fpr(), params[0].fpr());
1502     });
1503     patchpoint->effects = Effects::none();
1504     result = patchpoint;
1505     return { };
1506 }
1507
1508 template<>
1509 auto B3IRGenerator::addOp<OpType::F32Nearest>(ExpressionType arg, ExpressionType& result) -> PartialResult
1510 {
1511     PatchpointValue* patchpoint = m_currentBlock->appendNew<PatchpointValue>(m_proc, Float, origin());
1512     patchpoint->append(arg, ValueRep::SomeRegister);
1513     patchpoint->setGenerator([=] (CCallHelpers& jit, const StackmapGenerationParams& params) {
1514         jit.roundTowardNearestIntFloat(params[1].fpr(), params[0].fpr());
1515     });
1516     patchpoint->effects = Effects::none();
1517     result = patchpoint;
1518     return { };
1519 }
1520
1521 template<>
1522 auto B3IRGenerator::addOp<OpType::F64Trunc>(ExpressionType arg, ExpressionType& result) -> PartialResult
1523 {
1524     PatchpointValue* patchpoint = m_currentBlock->appendNew<PatchpointValue>(m_proc, Double, origin());
1525     patchpoint->append(arg, ValueRep::SomeRegister);
1526     patchpoint->setGenerator([=] (CCallHelpers& jit, const StackmapGenerationParams& params) {
1527         jit.roundTowardZeroDouble(params[1].fpr(), params[0].fpr());
1528     });
1529     patchpoint->effects = Effects::none();
1530     result = patchpoint;
1531     return { };
1532 }
1533
1534 template<>
1535 auto B3IRGenerator::addOp<OpType::F32Trunc>(ExpressionType arg, ExpressionType& result) -> PartialResult
1536 {
1537     PatchpointValue* patchpoint = m_currentBlock->appendNew<PatchpointValue>(m_proc, Float, origin());
1538     patchpoint->append(arg, ValueRep::SomeRegister);
1539     patchpoint->setGenerator([=] (CCallHelpers& jit, const StackmapGenerationParams& params) {
1540         jit.roundTowardZeroFloat(params[1].fpr(), params[0].fpr());
1541     });
1542     patchpoint->effects = Effects::none();
1543     result = patchpoint;
1544     return { };
1545 }
1546
1547 template<>
1548 auto B3IRGenerator::addOp<OpType::I32TruncSF64>(ExpressionType arg, ExpressionType& result) -> PartialResult
1549 {
1550     Value* max = constant(Double, bitwise_cast<uint64_t>(-static_cast<double>(std::numeric_limits<int32_t>::min())));
1551     Value* min = constant(Double, bitwise_cast<uint64_t>(static_cast<double>(std::numeric_limits<int32_t>::min())));
1552     Value* outOfBounds = m_currentBlock->appendNew<Value>(m_proc, BitAnd, origin(),
1553         m_currentBlock->appendNew<Value>(m_proc, LessThan, origin(), arg, max),
1554         m_currentBlock->appendNew<Value>(m_proc, GreaterEqual, origin(), arg, min));
1555     outOfBounds = m_currentBlock->appendNew<Value>(m_proc, Equal, origin(), outOfBounds, constant(Int32, 0));
1556     CheckValue* trap = m_currentBlock->appendNew<CheckValue>(m_proc, Check, origin(), outOfBounds);
1557     trap->setGenerator([=] (CCallHelpers& jit, const StackmapGenerationParams&) {
1558         this->emitExceptionCheck(jit, ExceptionType::OutOfBoundsTrunc);
1559     });
1560     PatchpointValue* patchpoint = m_currentBlock->appendNew<PatchpointValue>(m_proc, Int32, origin());
1561     patchpoint->append(arg, ValueRep::SomeRegister);
1562     patchpoint->setGenerator([=] (CCallHelpers& jit, const StackmapGenerationParams& params) {
1563         jit.truncateDoubleToInt32(params[1].fpr(), params[0].gpr());
1564     });
1565     patchpoint->effects = Effects::none();
1566     result = patchpoint;
1567     return { };
1568 }
1569
1570 template<>
1571 auto B3IRGenerator::addOp<OpType::I32TruncSF32>(ExpressionType arg, ExpressionType& result) -> PartialResult
1572 {
1573     Value* max = constant(Float, bitwise_cast<uint32_t>(-static_cast<float>(std::numeric_limits<int32_t>::min())));
1574     Value* min = constant(Float, bitwise_cast<uint32_t>(static_cast<float>(std::numeric_limits<int32_t>::min())));
1575     Value* outOfBounds = m_currentBlock->appendNew<Value>(m_proc, BitAnd, origin(),
1576         m_currentBlock->appendNew<Value>(m_proc, LessThan, origin(), arg, max),
1577         m_currentBlock->appendNew<Value>(m_proc, GreaterEqual, origin(), arg, min));
1578     outOfBounds = m_currentBlock->appendNew<Value>(m_proc, Equal, origin(), outOfBounds, constant(Int32, 0));
1579     CheckValue* trap = m_currentBlock->appendNew<CheckValue>(m_proc, Check, origin(), outOfBounds);
1580     trap->setGenerator([=] (CCallHelpers& jit, const StackmapGenerationParams&) {
1581         this->emitExceptionCheck(jit, ExceptionType::OutOfBoundsTrunc);
1582     });
1583     PatchpointValue* patchpoint = m_currentBlock->appendNew<PatchpointValue>(m_proc, Int32, origin());
1584     patchpoint->append(arg, ValueRep::SomeRegister);
1585     patchpoint->setGenerator([=] (CCallHelpers& jit, const StackmapGenerationParams& params) {
1586         jit.truncateFloatToInt32(params[1].fpr(), params[0].gpr());
1587     });
1588     patchpoint->effects = Effects::none();
1589     result = patchpoint;
1590     return { };
1591 }
1592
1593
1594 template<>
1595 auto B3IRGenerator::addOp<OpType::I32TruncUF64>(ExpressionType arg, ExpressionType& result) -> PartialResult
1596 {
1597     Value* max = constant(Double, bitwise_cast<uint64_t>(static_cast<double>(std::numeric_limits<int32_t>::min()) * -2.0));
1598     Value* min = constant(Double, bitwise_cast<uint64_t>(-1.0));
1599     Value* outOfBounds = m_currentBlock->appendNew<Value>(m_proc, BitAnd, origin(),
1600         m_currentBlock->appendNew<Value>(m_proc, LessThan, origin(), arg, max),
1601         m_currentBlock->appendNew<Value>(m_proc, GreaterThan, origin(), arg, min));
1602     outOfBounds = m_currentBlock->appendNew<Value>(m_proc, Equal, origin(), outOfBounds, constant(Int32, 0));
1603     CheckValue* trap = m_currentBlock->appendNew<CheckValue>(m_proc, Check, origin(), outOfBounds);
1604     trap->setGenerator([=] (CCallHelpers& jit, const StackmapGenerationParams&) {
1605         this->emitExceptionCheck(jit, ExceptionType::OutOfBoundsTrunc);
1606     });
1607     PatchpointValue* patchpoint = m_currentBlock->appendNew<PatchpointValue>(m_proc, Int32, origin());
1608     patchpoint->append(arg, ValueRep::SomeRegister);
1609     patchpoint->setGenerator([=] (CCallHelpers& jit, const StackmapGenerationParams& params) {
1610         jit.truncateDoubleToUint32(params[1].fpr(), params[0].gpr());
1611     });
1612     patchpoint->effects = Effects::none();
1613     result = patchpoint;
1614     return { };
1615 }
1616
1617 template<>
1618 auto B3IRGenerator::addOp<OpType::I32TruncUF32>(ExpressionType arg, ExpressionType& result) -> PartialResult
1619 {
1620     Value* max = constant(Float, bitwise_cast<uint32_t>(static_cast<float>(std::numeric_limits<int32_t>::min()) * static_cast<float>(-2.0)));
1621     Value* min = constant(Float, bitwise_cast<uint32_t>(static_cast<float>(-1.0)));
1622     Value* outOfBounds = m_currentBlock->appendNew<Value>(m_proc, BitAnd, origin(),
1623         m_currentBlock->appendNew<Value>(m_proc, LessThan, origin(), arg, max),
1624         m_currentBlock->appendNew<Value>(m_proc, GreaterThan, origin(), arg, min));
1625     outOfBounds = m_currentBlock->appendNew<Value>(m_proc, Equal, origin(), outOfBounds, constant(Int32, 0));
1626     CheckValue* trap = m_currentBlock->appendNew<CheckValue>(m_proc, Check, origin(), outOfBounds);
1627     trap->setGenerator([=] (CCallHelpers& jit, const StackmapGenerationParams&) {
1628         this->emitExceptionCheck(jit, ExceptionType::OutOfBoundsTrunc);
1629     });
1630     PatchpointValue* patchpoint = m_currentBlock->appendNew<PatchpointValue>(m_proc, Int32, origin());
1631     patchpoint->append(arg, ValueRep::SomeRegister);
1632     patchpoint->setGenerator([=] (CCallHelpers& jit, const StackmapGenerationParams& params) {
1633         jit.truncateFloatToUint32(params[1].fpr(), params[0].gpr());
1634     });
1635     patchpoint->effects = Effects::none();
1636     result = patchpoint;
1637     return { };
1638 }
1639
1640 template<>
1641 auto B3IRGenerator::addOp<OpType::I64TruncSF64>(ExpressionType arg, ExpressionType& result) -> PartialResult
1642 {
1643     Value* max = constant(Double, bitwise_cast<uint64_t>(-static_cast<double>(std::numeric_limits<int64_t>::min())));
1644     Value* min = constant(Double, bitwise_cast<uint64_t>(static_cast<double>(std::numeric_limits<int64_t>::min())));
1645     Value* outOfBounds = m_currentBlock->appendNew<Value>(m_proc, BitAnd, origin(),
1646         m_currentBlock->appendNew<Value>(m_proc, LessThan, origin(), arg, max),
1647         m_currentBlock->appendNew<Value>(m_proc, GreaterEqual, origin(), arg, min));
1648     outOfBounds = m_currentBlock->appendNew<Value>(m_proc, Equal, origin(), outOfBounds, constant(Int32, 0));
1649     CheckValue* trap = m_currentBlock->appendNew<CheckValue>(m_proc, Check, origin(), outOfBounds);
1650     trap->setGenerator([=] (CCallHelpers& jit, const StackmapGenerationParams&) {
1651         this->emitExceptionCheck(jit, ExceptionType::OutOfBoundsTrunc);
1652     });
1653     PatchpointValue* patchpoint = m_currentBlock->appendNew<PatchpointValue>(m_proc, Int64, origin());
1654     patchpoint->append(arg, ValueRep::SomeRegister);
1655     patchpoint->setGenerator([=] (CCallHelpers& jit, const StackmapGenerationParams& params) {
1656         jit.truncateDoubleToInt64(params[1].fpr(), params[0].gpr());
1657     });
1658     patchpoint->effects = Effects::none();
1659     result = patchpoint;
1660     return { };
1661 }
1662
1663 template<>
1664 auto B3IRGenerator::addOp<OpType::I64TruncUF64>(ExpressionType arg, ExpressionType& result) -> PartialResult
1665 {
1666     Value* max = constant(Double, bitwise_cast<uint64_t>(static_cast<double>(std::numeric_limits<int64_t>::min()) * -2.0));
1667     Value* min = constant(Double, bitwise_cast<uint64_t>(-1.0));
1668     Value* outOfBounds = m_currentBlock->appendNew<Value>(m_proc, BitAnd, origin(),
1669         m_currentBlock->appendNew<Value>(m_proc, LessThan, origin(), arg, max),
1670         m_currentBlock->appendNew<Value>(m_proc, GreaterThan, origin(), arg, min));
1671     outOfBounds = m_currentBlock->appendNew<Value>(m_proc, Equal, origin(), outOfBounds, constant(Int32, 0));
1672     CheckValue* trap = m_currentBlock->appendNew<CheckValue>(m_proc, Check, origin(), outOfBounds);
1673     trap->setGenerator([=] (CCallHelpers& jit, const StackmapGenerationParams&) {
1674         this->emitExceptionCheck(jit, ExceptionType::OutOfBoundsTrunc);
1675     });
1676
1677     Value* signBitConstant;
1678     if (isX86()) {
1679         // Since x86 doesn't have an instruction to convert floating points to unsigned integers, we at least try to do the smart thing if
1680         // the numbers are would be positive anyway as a signed integer. Since we cannot materialize constants into fprs we have b3 do it
1681         // so we can pool them if needed.
1682         signBitConstant = constant(Double, bitwise_cast<uint64_t>(static_cast<double>(std::numeric_limits<uint64_t>::max() - std::numeric_limits<int64_t>::max())));
1683     }
1684     PatchpointValue* patchpoint = m_currentBlock->appendNew<PatchpointValue>(m_proc, Int64, origin());
1685     patchpoint->append(arg, ValueRep::SomeRegister);
1686     if (isX86()) {
1687         patchpoint->append(signBitConstant, ValueRep::SomeRegister);
1688         patchpoint->numFPScratchRegisters = 1;
1689     }
1690     patchpoint->setGenerator([=] (CCallHelpers& jit, const StackmapGenerationParams& params) {
1691         AllowMacroScratchRegisterUsage allowScratch(jit);
1692         FPRReg scratch = InvalidFPRReg;
1693         FPRReg constant = InvalidFPRReg;
1694         if (isX86()) {
1695             scratch = params.fpScratch(0);
1696             constant = params[2].fpr();
1697         }
1698         jit.truncateDoubleToUint64(params[1].fpr(), params[0].gpr(), scratch, constant);
1699     });
1700     patchpoint->effects = Effects::none();
1701     result = patchpoint;
1702     return { };
1703 }
1704
1705 template<>
1706 auto B3IRGenerator::addOp<OpType::I64TruncSF32>(ExpressionType arg, ExpressionType& result) -> PartialResult
1707 {
1708     Value* max = constant(Float, bitwise_cast<uint32_t>(-static_cast<float>(std::numeric_limits<int64_t>::min())));
1709     Value* min = constant(Float, bitwise_cast<uint32_t>(static_cast<float>(std::numeric_limits<int64_t>::min())));
1710     Value* outOfBounds = m_currentBlock->appendNew<Value>(m_proc, BitAnd, origin(),
1711         m_currentBlock->appendNew<Value>(m_proc, LessThan, origin(), arg, max),
1712         m_currentBlock->appendNew<Value>(m_proc, GreaterEqual, origin(), arg, min));
1713     outOfBounds = m_currentBlock->appendNew<Value>(m_proc, Equal, origin(), outOfBounds, constant(Int32, 0));
1714     CheckValue* trap = m_currentBlock->appendNew<CheckValue>(m_proc, Check, origin(), outOfBounds);
1715     trap->setGenerator([=] (CCallHelpers& jit, const StackmapGenerationParams&) {
1716         this->emitExceptionCheck(jit, ExceptionType::OutOfBoundsTrunc);
1717     });
1718     PatchpointValue* patchpoint = m_currentBlock->appendNew<PatchpointValue>(m_proc, Int64, origin());
1719     patchpoint->append(arg, ValueRep::SomeRegister);
1720     patchpoint->setGenerator([=] (CCallHelpers& jit, const StackmapGenerationParams& params) {
1721         jit.truncateFloatToInt64(params[1].fpr(), params[0].gpr());
1722     });
1723     patchpoint->effects = Effects::none();
1724     result = patchpoint;
1725     return { };
1726 }
1727
1728 template<>
1729 auto B3IRGenerator::addOp<OpType::I64TruncUF32>(ExpressionType arg, ExpressionType& result) -> PartialResult
1730 {
1731     Value* max = constant(Float, bitwise_cast<uint32_t>(static_cast<float>(std::numeric_limits<int64_t>::min()) * static_cast<float>(-2.0)));
1732     Value* min = constant(Float, bitwise_cast<uint32_t>(static_cast<float>(-1.0)));
1733     Value* outOfBounds = m_currentBlock->appendNew<Value>(m_proc, BitAnd, origin(),
1734         m_currentBlock->appendNew<Value>(m_proc, LessThan, origin(), arg, max),
1735         m_currentBlock->appendNew<Value>(m_proc, GreaterThan, origin(), arg, min));
1736     outOfBounds = m_currentBlock->appendNew<Value>(m_proc, Equal, origin(), outOfBounds, constant(Int32, 0));
1737     CheckValue* trap = m_currentBlock->appendNew<CheckValue>(m_proc, Check, origin(), outOfBounds);
1738     trap->setGenerator([=] (CCallHelpers& jit, const StackmapGenerationParams&) {
1739         this->emitExceptionCheck(jit, ExceptionType::OutOfBoundsTrunc);
1740     });
1741
1742     Value* signBitConstant;
1743     if (isX86()) {
1744         // Since x86 doesn't have an instruction to convert floating points to unsigned integers, we at least try to do the smart thing if
1745         // the numbers would be positive anyway as a signed integer. Since we cannot materialize constants into fprs we have b3 do it
1746         // so we can pool them if needed.
1747         signBitConstant = constant(Float, bitwise_cast<uint32_t>(static_cast<float>(std::numeric_limits<uint64_t>::max() - std::numeric_limits<int64_t>::max())));
1748     }
1749     PatchpointValue* patchpoint = m_currentBlock->appendNew<PatchpointValue>(m_proc, Int64, origin());
1750     patchpoint->append(arg, ValueRep::SomeRegister);
1751     if (isX86()) {
1752         patchpoint->append(signBitConstant, ValueRep::SomeRegister);
1753         patchpoint->numFPScratchRegisters = 1;
1754     }
1755     patchpoint->setGenerator([=] (CCallHelpers& jit, const StackmapGenerationParams& params) {
1756         AllowMacroScratchRegisterUsage allowScratch(jit);
1757         FPRReg scratch = InvalidFPRReg;
1758         FPRReg constant = InvalidFPRReg;
1759         if (isX86()) {
1760             scratch = params.fpScratch(0);
1761             constant = params[2].fpr();
1762         }
1763         jit.truncateFloatToUint64(params[1].fpr(), params[0].gpr(), scratch, constant);
1764     });
1765     patchpoint->effects = Effects::none();
1766     result = patchpoint;
1767     return { };
1768 }
1769
1770 } } // namespace JSC::Wasm
1771
1772 #include "WasmB3IRGeneratorInlines.h"
1773
1774 #endif // ENABLE(WEBASSEMBLY)