LCOV - code coverage report
Current view: top level - src/wasm/baseline - liftoff-compiler.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 684 760 90.0 %
Date: 2019-02-19 Functions: 217 230 94.3 %

          Line data    Source code
       1             : // Copyright 2017 the V8 project authors. All rights reserved.
       2             : // Use of this source code is governed by a BSD-style license that can be
       3             : // found in the LICENSE file.
       4             : 
       5             : #include "src/wasm/baseline/liftoff-compiler.h"
       6             : 
       7             : #include "src/assembler-inl.h"
       8             : #include "src/base/optional.h"
       9             : // TODO(clemensh): Remove dependences on compiler stuff.
      10             : #include "src/compiler/linkage.h"
      11             : #include "src/compiler/wasm-compiler.h"
      12             : #include "src/counters.h"
      13             : #include "src/interface-descriptors.h"
      14             : #include "src/log.h"
      15             : #include "src/macro-assembler-inl.h"
      16             : #include "src/objects/smi.h"
      17             : #include "src/ostreams.h"
      18             : #include "src/tracing/trace-event.h"
      19             : #include "src/utils.h"
      20             : #include "src/wasm/baseline/liftoff-assembler.h"
      21             : #include "src/wasm/function-body-decoder-impl.h"
      22             : #include "src/wasm/function-compiler.h"
      23             : #include "src/wasm/memory-tracing.h"
      24             : #include "src/wasm/object-access.h"
      25             : #include "src/wasm/wasm-engine.h"
      26             : #include "src/wasm/wasm-linkage.h"
      27             : #include "src/wasm/wasm-objects.h"
      28             : #include "src/wasm/wasm-opcodes.h"
      29             : 
      30             : namespace v8 {
      31             : namespace internal {
      32             : namespace wasm {
      33             : 
      34             : constexpr auto kRegister = LiftoffAssembler::VarState::kRegister;
      35             : constexpr auto kIntConst = LiftoffAssembler::VarState::kIntConst;
      36             : constexpr auto kStack = LiftoffAssembler::VarState::kStack;
      37             : 
      38             : namespace {
      39             : 
      40             : #define __ asm_.
      41             : 
      42             : #define TRACE(...)                                            \
      43             :   do {                                                        \
      44             :     if (FLAG_trace_liftoff) PrintF("[liftoff] " __VA_ARGS__); \
      45             :   } while (false)
      46             : 
      47             : #define WASM_INSTANCE_OBJECT_FIELD_OFFSET(name) \
      48             :   ObjectAccess::ToTagged(WasmInstanceObject::k##name##Offset)
      49             : 
      50             : template <int expected_size, int actual_size>
      51             : struct assert_field_size {
      52             :   static_assert(expected_size == actual_size,
      53             :                 "field in WasmInstance does not have the expected size");
      54             :   static constexpr int size = actual_size;
      55             : };
      56             : 
      57             : #define WASM_INSTANCE_OBJECT_FIELD_SIZE(name) \
      58             :   FIELD_SIZE(WasmInstanceObject::k##name##Offset)
      59             : 
      60             : #define LOAD_INSTANCE_FIELD(dst, name, load_size)                              \
      61             :   __ LoadFromInstance(dst, WASM_INSTANCE_OBJECT_FIELD_OFFSET(name),            \
      62             :                       assert_field_size<WASM_INSTANCE_OBJECT_FIELD_SIZE(name), \
      63             :                                         load_size>::size);
      64             : 
      65             : #define LOAD_TAGGED_PTR_INSTANCE_FIELD(dst, name)                         \
      66             :   static_assert(WASM_INSTANCE_OBJECT_FIELD_SIZE(name) == kTaggedSize,     \
      67             :                 "field in WasmInstance does not have the expected size"); \
      68             :   __ LoadTaggedPointerFromInstance(dst,                                   \
      69             :                                    WASM_INSTANCE_OBJECT_FIELD_OFFSET(name));
      70             : 
      71             : #ifdef DEBUG
      72             : #define DEBUG_CODE_COMMENT(str) \
      73             :   do {                          \
      74             :     __ RecordComment(str);      \
      75             :   } while (false)
      76             : #else
      77             : #define DEBUG_CODE_COMMENT(str) ((void)0)
      78             : #endif
      79             : 
      80             : constexpr LoadType::LoadTypeValue kPointerLoadType =
      81             :     kSystemPointerSize == 8 ? LoadType::kI64Load : LoadType::kI32Load;
      82             : 
      83             : #if V8_TARGET_ARCH_ARM64
      84             : // On ARM64, the Assembler keeps track of pointers to Labels to resolve
      85             : // branches to distant targets. Moving labels would confuse the Assembler,
      86             : // thus store the label on the heap and keep a unique_ptr.
      87             : class MovableLabel {
      88             :  public:
      89             :   MOVE_ONLY_NO_DEFAULT_CONSTRUCTOR(MovableLabel);
      90             :   MovableLabel() : label_(new Label()) {}
      91             : 
      92             :   Label* get() { return label_.get(); }
      93             : 
      94             :  private:
      95             :   std::unique_ptr<Label> label_;
      96             : };
      97             : #else
      98             : // On all other platforms, just store the Label directly.
      99             : class MovableLabel {
     100             :  public:
     101             :   MOVE_ONLY_WITH_DEFAULT_CONSTRUCTORS(MovableLabel);
     102             : 
     103             :   Label* get() { return &label_; }
     104             : 
     105             :  private:
     106             :   Label label_;
     107             : };
     108             : #endif
     109             : 
     110             : compiler::CallDescriptor* GetLoweredCallDescriptor(
     111             :     Zone* zone, compiler::CallDescriptor* call_desc) {
     112             :   return kSystemPointerSize == 4
     113             :              ? compiler::GetI32WasmCallDescriptor(zone, call_desc)
     114             :              : call_desc;
     115             : }
     116             : 
     117             : constexpr ValueType kSupportedTypesArr[] = {kWasmI32, kWasmI64, kWasmF32,
     118             :                                             kWasmF64};
     119             : constexpr Vector<const ValueType> kSupportedTypes =
     120             :     ArrayVector(kSupportedTypesArr);
     121             : 
     122     1188031 : class LiftoffCompiler {
     123             :  public:
     124             :   // TODO(clemensh): Make this a template parameter.
     125             :   static constexpr Decoder::ValidateFlag validate = Decoder::kValidate;
     126             : 
     127             :   using Value = ValueBase;
     128             : 
     129             :   struct ElseState {
     130             :     MovableLabel label;
     131             :     LiftoffAssembler::CacheState state;
     132             :   };
     133             : 
     134     1773456 :   struct Control : public ControlBase<Value> {
     135             :     std::unique_ptr<ElseState> else_state;
     136             :     LiftoffAssembler::CacheState label_state;
     137             :     MovableLabel label;
     138             : 
     139      302010 :     MOVE_ONLY_NO_DEFAULT_CONSTRUCTOR(Control);
     140             : 
     141             :     template <typename... Args>
     142      735844 :     explicit Control(Args&&... args) V8_NOEXCEPT
     143     2207532 :         : ControlBase(std::forward<Args>(args)...) {}
     144             :   };
     145             : 
     146             :   using FullDecoder = WasmFullDecoder<validate, LiftoffCompiler>;
     147             : 
     148             :   struct OutOfLineCode {
     149             :     MovableLabel label;
     150             :     MovableLabel continuation;
     151             :     WasmCode::RuntimeStubId stub;
     152             :     WasmCodePosition position;
     153             :     LiftoffRegList regs_to_save;
     154             :     uint32_t pc;  // for trap handler.
     155             : 
     156             :     // Named constructors:
     157             :     static OutOfLineCode Trap(WasmCode::RuntimeStubId s, WasmCodePosition pos,
     158             :                               uint32_t pc) {
     159             :       DCHECK_LT(0, pos);
     160      385759 :       return {{}, {}, s, pos, {}, pc};
     161             :     }
     162             :     static OutOfLineCode StackCheck(WasmCodePosition pos, LiftoffRegList regs) {
     163      234402 :       return {{}, {}, WasmCode::kWasmStackGuard, pos, regs, 0};
     164             :     }
     165             :   };
     166             : 
     167      594168 :   LiftoffCompiler(compiler::CallDescriptor* call_descriptor,
     168             :                   CompilationEnv* env, Zone* compilation_zone,
     169             :                   std::unique_ptr<AssemblerBuffer> buffer)
     170             :       : asm_(std::move(buffer)),
     171             :         descriptor_(
     172             :             GetLoweredCallDescriptor(compilation_zone, call_descriptor)),
     173             :         env_(env),
     174             :         compilation_zone_(compilation_zone),
     175     2970882 :         safepoint_table_builder_(compilation_zone_) {}
     176             : 
     177             :   bool ok() const { return ok_; }
     178             : 
     179             :   void GetCode(CodeDesc* desc) {
     180             :     asm_.GetCode(nullptr, desc, &safepoint_table_builder_,
     181      570074 :                  Assembler::kNoHandlerTable);
     182             :   }
     183             : 
     184             :   OwnedVector<uint8_t> GetSourcePositionTable() {
     185      570041 :     return source_position_table_builder_.ToSourcePositionTableVector();
     186             :   }
     187             : 
     188             :   OwnedVector<trap_handler::ProtectedInstructionData> GetProtectedInstructions()
     189             :       const {
     190             :     return OwnedVector<trap_handler::ProtectedInstructionData>::Of(
     191      570133 :         protected_instructions_);
     192             :   }
     193             : 
     194             :   uint32_t GetTotalFrameSlotCount() const {
     195      570135 :     return __ GetTotalFrameSlotCount();
     196             :   }
     197             : 
     198             :   void unsupported(FullDecoder* decoder, const char* reason) {
     199       17278 :     ok_ = false;
     200             :     TRACE("unsupported: %s\n", reason);
     201             :     decoder->errorf(decoder->pc_offset(), "unsupported liftoff operation: %s",
     202       17278 :                     reason);
     203             :     UnuseLabels(decoder);
     204             :   }
     205             : 
     206     1733745 :   bool DidAssemblerBailout(FullDecoder* decoder) {
     207     1733745 :     if (decoder->failed() || !__ did_bailout()) return false;
     208             :     unsupported(decoder, __ bailout_reason());
     209           0 :     return true;
     210             :   }
     211             : 
     212      960134 :   bool CheckSupportedType(FullDecoder* decoder,
     213             :                           Vector<const ValueType> supported_types,
     214             :                           ValueType type, const char* context) {
     215             :     char buffer[128];
     216             :     // Check supported types.
     217     2582736 :     for (ValueType supported : supported_types) {
     218     2582192 :       if (type == supported) return true;
     219             :     }
     220         544 :     SNPrintF(ArrayVector(buffer), "%s %s", ValueTypes::TypeName(type), context);
     221             :     unsupported(decoder, buffer);
     222         542 :     return false;
     223             :   }
     224             : 
     225             :   int GetSafepointTableOffset() const {
     226             :     return safepoint_table_builder_.GetCodeOffset();
     227             :   }
     228             : 
     229             :   void UnuseLabels(FullDecoder* decoder) {
     230             : #ifdef DEBUG
     231             :     auto Unuse = [](Label* label) {
     232             :       label->Unuse();
     233             :       label->UnuseNear();
     234             :     };
     235             :     // Unuse all labels now, otherwise their destructor will fire a DCHECK error
     236             :     // if they where referenced before.
     237             :     uint32_t control_depth = decoder ? decoder->control_depth() : 0;
     238             :     for (uint32_t i = 0; i < control_depth; ++i) {
     239             :       Control* c = decoder->control_at(i);
     240             :       Unuse(c->label.get());
     241             :       if (c->else_state) Unuse(c->else_state->label.get());
     242             :     }
     243             :     for (auto& ool : out_of_line_code_) Unuse(ool.label.get());
     244             : #endif
     245             :   }
     246             : 
     247      594169 :   void StartFunction(FullDecoder* decoder) {
     248      594169 :     int num_locals = decoder->num_locals();
     249      594169 :     __ set_num_locals(num_locals);
     250     1239281 :     for (int i = 0; i < num_locals; ++i) {
     251      645112 :       __ set_local_type(i, decoder->GetLocalType(i));
     252             :     }
     253      594169 :   }
     254             : 
     255             :   // Returns the number of inputs processed (1 or 2).
     256      166309 :   uint32_t ProcessParameter(ValueType type, uint32_t input_idx) {
     257             :     const int num_lowered_params = 1 + needs_reg_pair(type);
     258             :     ValueType lowered_type = needs_reg_pair(type) ? kWasmI32 : type;
     259             :     RegClass rc = reg_class_for(lowered_type);
     260             :     // Initialize to anything, will be set in the loop and used afterwards.
     261             :     LiftoffRegister reg = kGpCacheRegList.GetFirstRegSet();
     262             :     LiftoffRegList pinned;
     263      332629 :     for (int pair_idx = 0; pair_idx < num_lowered_params; ++pair_idx) {
     264             :       compiler::LinkageLocation param_loc =
     265      166323 :           descriptor_->GetInputLocation(input_idx + pair_idx);
     266             :       // Initialize to anything, will be set in both arms of the if.
     267             :       LiftoffRegister in_reg = kGpCacheRegList.GetFirstRegSet();
     268      166322 :       if (param_loc.IsRegister()) {
     269             :         DCHECK(!param_loc.IsAnyRegister());
     270             :         int reg_code = param_loc.AsRegister();
     271             : #if V8_TARGET_ARCH_ARM
     272             :         // Liftoff assumes a one-to-one mapping between float registers and
     273             :         // double registers, and so does not distinguish between f32 and f64
     274             :         // registers. The f32 register code must therefore be halved in order to
     275             :         // pass the f64 code to Liftoff.
     276             :         DCHECK_IMPLIES(type == kWasmF32, (reg_code % 2) == 0);
     277             :         if (type == kWasmF32) {
     278             :           reg_code /= 2;
     279             :         }
     280             : #endif
     281             :         RegList cache_regs = rc == kGpReg ? kLiftoffAssemblerGpCacheRegs
     282      135026 :                                           : kLiftoffAssemblerFpCacheRegs;
     283      135026 :         if (cache_regs & (1ULL << reg_code)) {
     284             :           // This is a cache register, just use it.
     285      135026 :           in_reg = LiftoffRegister::from_code(rc, reg_code);
     286             :         } else {
     287             :           // Move to a cache register (spill one if necessary).
     288             :           // Note that we cannot create a {LiftoffRegister} for reg_code, since
     289             :           // {LiftoffRegister} can only store cache regs.
     290           0 :           in_reg = __ GetUnusedRegister(rc, pinned);
     291           0 :           if (rc == kGpReg) {
     292           0 :             __ Move(in_reg.gp(), Register::from_code(reg_code), lowered_type);
     293             :           } else {
     294             :             __ Move(in_reg.fp(), DoubleRegister::from_code(reg_code),
     295           0 :                     lowered_type);
     296             :           }
     297             :         }
     298       31296 :       } else if (param_loc.IsCallerFrameSlot()) {
     299       31295 :         in_reg = __ GetUnusedRegister(rc, pinned);
     300             :         __ LoadCallerFrameSlot(in_reg, -param_loc.AsCallerFrameSlot(),
     301       62590 :                                lowered_type);
     302             :       }
     303             :       reg = pair_idx == 0 ? in_reg
     304      166320 :                           : LiftoffRegister::ForPair(reg.gp(), in_reg.gp());
     305             :       pinned.set(reg);
     306             :     }
     307             :     __ PushRegister(type, reg);
     308      166323 :     return num_lowered_params;
     309             :   }
     310             : 
     311      595902 :   void StackCheck(WasmCodePosition position) {
     312      957402 :     if (FLAG_wasm_no_stack_checks || !env_->runtime_exception_support) return;
     313             :     out_of_line_code_.push_back(
     314      468928 :         OutOfLineCode::StackCheck(position, __ cache_state()->used_registers));
     315             :     OutOfLineCode& ool = out_of_line_code_.back();
     316      469036 :     Register limit_address = __ GetUnusedRegister(kGpReg).gp();
     317      234510 :     LOAD_INSTANCE_FIELD(limit_address, StackLimitAddress, kSystemPointerSize);
     318      234482 :     __ StackCheck(ool.label.get(), limit_address);
     319      234439 :     __ bind(ool.continuation.get());
     320             :   }
     321             : 
     322      594091 :   void StartFunctionBody(FullDecoder* decoder, Control* block) {
     323     1879603 :     for (uint32_t i = 0; i < __ num_locals(); ++i) {
     324     2950031 :       if (!CheckSupportedType(decoder, kSupportedTypes, __ local_type(i),
     325      643126 :                               "param"))
     326         400 :         return;
     327             :     }
     328             : 
     329             :     // Input 0 is the call target, the instance is at 1.
     330             :     constexpr int kInstanceParameterIndex = 1;
     331             :     // Store the instance parameter to a special stack slot.
     332             :     compiler::LinkageLocation instance_loc =
     333      593721 :         descriptor_->GetInputLocation(kInstanceParameterIndex);
     334             :     DCHECK(instance_loc.IsRegister());
     335             :     DCHECK(!instance_loc.IsAnyRegister());
     336      593716 :     Register instance_reg = Register::from_code(instance_loc.AsRegister());
     337             :     DCHECK_EQ(kWasmInstanceRegister, instance_reg);
     338             : 
     339             :     // Parameter 0 is the instance parameter.
     340             :     uint32_t num_params =
     341      593716 :         static_cast<uint32_t>(decoder->sig_->parameter_count());
     342             : 
     343      593716 :     __ EnterFrame(StackFrame::WASM_COMPILED);
     344             :     __ set_has_frame(true);
     345      593699 :     pc_offset_stack_frame_construction_ = __ PrepareStackFrame();
     346             :     // {PrepareStackFrame} is the first platform-specific assembler method.
     347             :     // If this failed, we can bail out immediately, avoiding runtime overhead
     348             :     // and potential failures because of other unimplemented methods.
     349             :     // A platform implementing {PrepareStackFrame} must ensure that we can
     350             :     // finish compilation without errors even if we hit unimplemented
     351             :     // LiftoffAssembler methods.
     352      593699 :     if (DidAssemblerBailout(decoder)) return;
     353             : 
     354      593657 :     __ SpillInstance(instance_reg);
     355             :     // Input 0 is the code target, 1 is the instance. First parameter at 2.
     356             :     uint32_t input_idx = kInstanceParameterIndex + 1;
     357      166323 :     for (uint32_t param_idx = 0; param_idx < num_params; ++param_idx) {
     358      166331 :       input_idx += ProcessParameter(__ local_type(param_idx), input_idx);
     359             :     }
     360             :     DCHECK_EQ(input_idx, descriptor_->InputCount());
     361             :     // Set to a gp register, to mark this uninitialized.
     362             :     LiftoffRegister zero_double_reg = kGpCacheRegList.GetFirstRegSet();
     363             :     DCHECK(zero_double_reg.is_gp());
     364     1546365 :     for (uint32_t param_idx = num_params; param_idx < __ num_locals();
     365             :          ++param_idx) {
     366             :       ValueType type = decoder->GetLocalType(param_idx);
     367      476336 :       switch (type) {
     368             :         case kWasmI32:
     369       37666 :           __ cache_state()->stack_state.emplace_back(kWasmI32, uint32_t{0});
     370             :           break;
     371             :         case kWasmI64:
     372       40919 :           __ cache_state()->stack_state.emplace_back(kWasmI64, uint32_t{0});
     373             :           break;
     374             :         case kWasmF32:
     375             :         case kWasmF64:
     376      397751 :           if (zero_double_reg.is_gp()) {
     377             :             // Note: This might spill one of the registers used to hold
     378             :             // parameters.
     379             :             zero_double_reg = __ GetUnusedRegister(kFpReg);
     380             :             // Zero is represented by the bit pattern 0 for both f32 and f64.
     381         755 :             __ LoadConstant(zero_double_reg, WasmValue(0.));
     382             :           }
     383             :           __ PushRegister(type, zero_double_reg);
     384             :           break;
     385             :         default:
     386           0 :           UNIMPLEMENTED();
     387             :       }
     388             :     }
     389             : 
     390             :     // The function-prologue stack check is associated with position 0, which
     391             :     // is never a position of any instruction in the function.
     392      593692 :     StackCheck(0);
     393             : 
     394             :     DCHECK_EQ(__ num_locals(), __ cache_state()->stack_height());
     395             :   }
     396             : 
     397      569805 :   void GenerateOutOfLineCode(OutOfLineCode& ool) {
     398      569805 :     __ bind(ool.label.get());
     399      569784 :     const bool is_stack_check = ool.stub == WasmCode::kWasmStackGuard;
     400             :     const bool is_mem_out_of_bounds =
     401             :         ool.stub == WasmCode::kThrowWasmTrapMemOutOfBounds;
     402             : 
     403      569784 :     if (is_mem_out_of_bounds && env_->use_trap_handler) {
     404      696374 :       uint32_t pc = static_cast<uint32_t>(__ pc_offset());
     405             :       DCHECK_EQ(pc, __ pc_offset());
     406             :       protected_instructions_.emplace_back(
     407      172100 :           trap_handler::ProtectedInstructionData{ool.pc, pc});
     408             :     }
     409             : 
     410      569755 :     if (!env_->runtime_exception_support) {
     411             :       // We cannot test calls to the runtime in cctest/test-run-wasm.
     412             :       // Therefore we emit a call to C here instead of a call to the runtime.
     413             :       // In this mode, we never generate stack checks.
     414             :       DCHECK(!is_stack_check);
     415      217624 :       __ CallTrapCallbackForTesting();
     416      217624 :       __ LeaveFrame(StackFrame::WASM_COMPILED);
     417             :       __ DropStackSlotsAndRet(
     418      217624 :           static_cast<uint32_t>(descriptor_->StackParameterCount()));
     419      569868 :       return;
     420             :     }
     421             : 
     422      352131 :     if (!ool.regs_to_save.is_empty()) __ PushRegisters(ool.regs_to_save);
     423             : 
     424             :     source_position_table_builder_.AddPosition(
     425      704348 :         __ pc_offset(), SourcePosition(ool.position), false);
     426      352349 :     __ CallRuntimeStub(ool.stub);
     427             :     safepoint_table_builder_.DefineSafepoint(&asm_, Safepoint::kSimple,
     428      352162 :                                              Safepoint::kNoLazyDeopt);
     429             :     DCHECK_EQ(ool.continuation.get()->is_bound(), is_stack_check);
     430      352318 :     if (!ool.regs_to_save.is_empty()) __ PopRegisters(ool.regs_to_save);
     431      352304 :     if (is_stack_check) {
     432      212175 :       __ emit_jump(ool.continuation.get());
     433             :     } else {
     434             :       __ AssertUnreachable(AbortReason::kUnexpectedReturnFromWasmTrap);
     435             :     }
     436             :   }
     437             : 
     438      570091 :   void FinishFunction(FullDecoder* decoder) {
     439     1140293 :     if (DidAssemblerBailout(decoder)) return;
     440     1710220 :     for (OutOfLineCode& ool : out_of_line_code_) {
     441      569809 :       GenerateOutOfLineCode(ool);
     442             :     }
     443             :     __ PatchPrepareStackFrame(pc_offset_stack_frame_construction_,
     444     1140464 :                               __ GetTotalFrameSlotCount());
     445             :     __ FinishCode();
     446      570289 :     safepoint_table_builder_.Emit(&asm_, __ GetTotalFrameSlotCount());
     447             :     __ MaybeEmitOutOfLineConstantPool();
     448             :     // The previous calls may have also generated a bailout.
     449      570231 :     DidAssemblerBailout(decoder);
     450             :   }
     451             : 
     452             :   void OnFirstError(FullDecoder* decoder) {
     453       47807 :     ok_ = false;
     454             :     UnuseLabels(decoder);
     455             :     asm_.AbortCompilation();
     456             :   }
     457             : 
     458             :   void NextInstruction(FullDecoder* decoder, WasmOpcode opcode) {
     459             :     TraceCacheState(decoder);
     460             :     SLOW_DCHECK(__ ValidateCacheState());
     461             :     DEBUG_CODE_COMMENT(WasmOpcodes::OpcodeName(opcode));
     462             :   }
     463             : 
     464             :   void Block(FullDecoder* decoder, Control* block) {}
     465             : 
     466        4459 :   void Loop(FullDecoder* decoder, Control* loop) {
     467             :     // Before entering a loop, spill all locals to the stack, in order to free
     468             :     // the cache registers, and to avoid unnecessarily reloading stack values
     469             :     // into registers at branches.
     470             :     // TODO(clemensh): Come up with a better strategy here, involving
     471             :     // pre-analysis of the function.
     472        2230 :     __ SpillLocals();
     473             : 
     474             :     // Loop labels bind at the beginning of the block.
     475        2230 :     __ bind(loop->label.get());
     476             : 
     477             :     // Save the current cache state for the merge when jumping to this loop.
     478        2229 :     loop->label_state.Split(*__ cache_state());
     479             : 
     480             :     // Execute a stack check in the loop header.
     481        2229 :     StackCheck(decoder->position());
     482        2229 :   }
     483             : 
     484         236 :   void Try(FullDecoder* decoder, Control* block) {
     485             :     unsupported(decoder, "try");
     486         236 :   }
     487             : 
     488           0 :   void Catch(FullDecoder* decoder, Control* block, Value* exception) {
     489             :     unsupported(decoder, "catch");
     490           0 :   }
     491             : 
     492        3090 :   void If(FullDecoder* decoder, const Value& cond, Control* if_block) {
     493             :     DCHECK_EQ(if_block, decoder->control_at(0));
     494             :     DCHECK(if_block->is_if());
     495             : 
     496        3090 :     if (if_block->start_merge.arity > 0 || if_block->end_merge.arity > 1)
     497        3121 :       return unsupported(decoder, "multi-value if");
     498             : 
     499             :     // Allocate the else state.
     500        6131 :     if_block->else_state = base::make_unique<ElseState>();
     501             : 
     502             :     // Test the condition, jump to else if zero.
     503        6136 :     Register value = __ PopToRegister().gp();
     504             :     __ emit_cond_jump(kEqual, if_block->else_state->label.get(), kWasmI32,
     505        3068 :                       value);
     506             : 
     507             :     // Store the state (after popping the value) for executing the else branch.
     508        6132 :     if_block->else_state->state.Split(*__ cache_state());
     509             :   }
     510             : 
     511           0 :   void FallThruTo(FullDecoder* decoder, Control* c) {
     512           0 :     if (c->end_merge.reached) {
     513           0 :       __ MergeFullStackWith(c->label_state, *__ cache_state());
     514             :     } else {
     515           0 :       c->label_state.Split(*__ cache_state());
     516             :     }
     517             :     TraceCacheState(decoder);
     518           0 :   }
     519             : 
     520        1121 :   void FinishOneArmedIf(FullDecoder* decoder, Control* c) {
     521             :     DCHECK(c->is_onearmed_if());
     522        1121 :     if (c->end_merge.reached) {
     523             :       // Someone already merged to the end of the if. Merge both arms into that.
     524        1121 :       if (c->reachable()) {
     525             :         // Merge the if state into the end state.
     526         592 :         __ MergeFullStackWith(c->label_state, *__ cache_state());
     527           0 :         __ emit_jump(c->label.get());
     528             :       }
     529             :       // Merge the else state into the end state.
     530          52 :       __ bind(c->else_state->label.get());
     531          52 :       __ MergeFullStackWith(c->label_state, c->else_state->state);
     532          52 :       __ cache_state()->Steal(c->label_state);
     533        1069 :     } else if (c->reachable()) {
     534             :       // No merge yet at the end of the if, but we need to create a merge for
     535             :       // the both arms of this if. Thus init the merge point from the else
     536             :       // state, then merge the if state into that.
     537             :       DCHECK_EQ(0, c->end_merge.arity);
     538             :       c->label_state.InitMerge(c->else_state->state, __ num_locals(), 0,
     539        1184 :                                c->stack_depth);
     540         592 :       __ MergeFullStackWith(c->label_state, *__ cache_state());
     541         592 :       __ emit_jump(c->label.get());
     542             :       // Merge the else state into the end state.
     543         592 :       __ bind(c->else_state->label.get());
     544         592 :       __ MergeFullStackWith(c->label_state, c->else_state->state);
     545         592 :       __ cache_state()->Steal(c->label_state);
     546             :     } else {
     547             :       // No merge needed, just continue with the else state.
     548         477 :       __ bind(c->else_state->label.get());
     549         477 :       __ cache_state()->Steal(c->else_state->state);
     550             :     }
     551        1121 :   }
     552             : 
     553      138487 :   void PopControl(FullDecoder* decoder, Control* c) {
     554      398216 :     if (c->is_loop()) return;  // A loop just falls through.
     555      136548 :     if (c->is_onearmed_if()) {
     556             :       // Special handling for one-armed ifs.
     557        1121 :       FinishOneArmedIf(decoder, c);
     558      135427 :     } else if (c->end_merge.reached) {
     559             :       // There is a merge already. Merge our state into that, then continue with
     560             :       // that state.
     561      121244 :       if (c->reachable()) {
     562        2296 :         __ MergeFullStackWith(c->label_state, *__ cache_state());
     563             :       }
     564      121243 :       __ cache_state()->Steal(c->label_state);
     565             :     } else {
     566             :       // No merge, just continue with our current state.
     567             :     }
     568             : 
     569      136546 :     if (!c->label.get()->is_bound()) __ bind(c->label.get());
     570             :   }
     571             : 
     572             :   void EndControl(FullDecoder* decoder, Control* c) {}
     573             : 
     574             :   enum CCallReturn : bool { kHasReturn = true, kNoReturn = false };
     575             : 
     576       26952 :   void GenerateCCall(const LiftoffRegister* result_regs, FunctionSig* sig,
     577             :                      ValueType out_argument_type,
     578             :                      const LiftoffRegister* arg_regs,
     579             :                      ExternalReference ext_ref) {
     580             :     // Before making a call, spill all cache registers.
     581       26952 :     __ SpillAllRegisters();
     582             : 
     583             :     // Store arguments on our stack, then align the stack for calling to C.
     584       26952 :     int param_bytes = 0;
     585       80856 :     for (ValueType param_type : sig->parameters()) {
     586       53904 :       param_bytes += ValueTypes::MemSize(param_type);
     587             :     }
     588             :     int out_arg_bytes = out_argument_type == kWasmStmt
     589             :                             ? 0
     590       26952 :                             : ValueTypes::MemSize(out_argument_type);
     591       26952 :     int stack_bytes = std::max(param_bytes, out_arg_bytes);
     592             :     __ CallC(sig, arg_regs, result_regs, out_argument_type, stack_bytes,
     593       26952 :              ext_ref);
     594       26952 :   }
     595             : 
     596             :   template <ValueType src_type, ValueType result_type, class EmitFn>
     597      107626 :   void EmitUnOp(EmitFn fn) {
     598             :     static RegClass src_rc = reg_class_for(src_type);
     599             :     static RegClass result_rc = reg_class_for(result_type);
     600      107626 :     LiftoffRegister src = __ PopToRegister();
     601             :     LiftoffRegister dst = src_rc == result_rc
     602      322880 :                               ? __ GetUnusedRegister(result_rc, {src})
     603      215252 :                               : __ GetUnusedRegister(result_rc);
     604         516 :     fn(dst, src);
     605             :     __ PushRegister(result_type, dst);
     606      107626 :   }
     607             : 
     608          76 :   void EmitI32UnOpWithCFallback(bool (LiftoffAssembler::*emit_fn)(Register,
     609             :                                                                   Register),
     610             :                                 ExternalReference (*fallback_fn)()) {
     611          76 :     auto emit_with_c_fallback = [=](LiftoffRegister dst, LiftoffRegister src) {
     612         228 :       if (emit_fn && (asm_.*emit_fn)(dst.gp(), src.gp())) return;
     613           0 :       ExternalReference ext_ref = fallback_fn();
     614           0 :       ValueType sig_i_i_reps[] = {kWasmI32, kWasmI32};
     615             :       FunctionSig sig_i_i(1, 1, sig_i_i_reps);
     616           0 :       GenerateCCall(&dst, &sig_i_i, kWasmStmt, &src, ext_ref);
     617          76 :     };
     618          76 :     EmitUnOp<kWasmI32, kWasmI32>(emit_with_c_fallback);
     619          76 :   }
     620             : 
     621             :   template <ValueType type>
     622         160 :   void EmitFloatUnOpWithCFallback(
     623             :       bool (LiftoffAssembler::*emit_fn)(DoubleRegister, DoubleRegister),
     624             :       ExternalReference (*fallback_fn)()) {
     625         160 :     auto emit_with_c_fallback = [=](LiftoffRegister dst, LiftoffRegister src) {
     626         478 :       if ((asm_.*emit_fn)(dst.fp(), src.fp())) return;
     627           0 :       ExternalReference ext_ref = fallback_fn();
     628           0 :       ValueType sig_reps[] = {type};
     629             :       FunctionSig sig(0, 1, sig_reps);
     630           0 :       GenerateCCall(&dst, &sig, type, &src, ext_ref);
     631         160 :     };
     632         160 :     EmitUnOp<type, type>(emit_with_c_fallback);
     633         158 :   }
     634             : 
     635             :   enum TypeConversionTrapping : bool { kCanTrap = true, kNoTrap = false };
     636             :   template <ValueType dst_type, ValueType src_type,
     637             :             TypeConversionTrapping can_trap>
     638      117414 :   void EmitTypeConversion(WasmOpcode opcode, ExternalReference (*fallback_fn)(),
     639             :                           WasmCodePosition trap_position) {
     640             :     static constexpr RegClass src_rc = reg_class_for(src_type);
     641             :     static constexpr RegClass dst_rc = reg_class_for(dst_type);
     642      117414 :     LiftoffRegister src = __ PopToRegister();
     643       28051 :     LiftoffRegister dst = src_rc == dst_rc ? __ GetUnusedRegister(dst_rc, {src})
     644      116361 :                                            : __ GetUnusedRegister(dst_rc);
     645             :     DCHECK_EQ(!!can_trap, trap_position > 0);
     646             :     Label* trap = can_trap ? AddOutOfLineTrap(
     647             :                                  trap_position,
     648             :                                  WasmCode::kThrowWasmTrapFloatUnrepresentable)
     649        1061 :                            : nullptr;
     650      117424 :     if (!__ emit_type_conversion(opcode, dst, src, trap)) {
     651             :       DCHECK_NOT_NULL(fallback_fn);
     652           0 :       ExternalReference ext_ref = fallback_fn();
     653             :       if (can_trap) {
     654             :         // External references for potentially trapping conversions return int.
     655           0 :         ValueType sig_reps[] = {kWasmI32, src_type};
     656             :         FunctionSig sig(1, 1, sig_reps);
     657             :         LiftoffRegister ret_reg =
     658             :             __ GetUnusedRegister(kGpReg, LiftoffRegList::ForRegs(dst));
     659           0 :         LiftoffRegister dst_regs[] = {ret_reg, dst};
     660           0 :         GenerateCCall(dst_regs, &sig, dst_type, &src, ext_ref);
     661           0 :         __ emit_cond_jump(kEqual, trap, kWasmI32, ret_reg.gp());
     662             :       } else {
     663           0 :         ValueType sig_reps[] = {src_type};
     664             :         FunctionSig sig(0, 1, sig_reps);
     665           0 :         GenerateCCall(&dst, &sig, dst_type, &src, ext_ref);
     666             :       }
     667             :     }
     668             :     __ PushRegister(dst_type, dst);
     669      117423 :   }
     670             : 
     671      226252 :   void UnOp(FullDecoder* decoder, WasmOpcode opcode, const Value& value,
     672             :             Value* result) {
     673             : #define CASE_I32_UNOP(opcode, fn)                       \
     674             :   case WasmOpcode::kExpr##opcode:                       \
     675             :     EmitUnOp<kWasmI32, kWasmI32>(                       \
     676             :         [=](LiftoffRegister dst, LiftoffRegister src) { \
     677             :           __ emit_##fn(dst.gp(), src.gp());             \
     678             :         });                                             \
     679             :     break;
     680             : #define CASE_I32_SIGN_EXTENSION(opcode, fn)             \
     681             :   case WasmOpcode::kExpr##opcode:                       \
     682             :     EmitUnOp<kWasmI32, kWasmI32>(                       \
     683             :         [=](LiftoffRegister dst, LiftoffRegister src) { \
     684             :           __ emit_##fn(dst.gp(), src.gp());             \
     685             :         });                                             \
     686             :     break;
     687             : #define CASE_I64_SIGN_EXTENSION(opcode, fn)             \
     688             :   case WasmOpcode::kExpr##opcode:                       \
     689             :     EmitUnOp<kWasmI64, kWasmI64>(                       \
     690             :         [=](LiftoffRegister dst, LiftoffRegister src) { \
     691             :           __ emit_##fn(dst, src);                       \
     692             :         });                                             \
     693             :     break;
     694             : #define CASE_FLOAT_UNOP(opcode, type, fn)               \
     695             :   case WasmOpcode::kExpr##opcode:                       \
     696             :     EmitUnOp<kWasm##type, kWasm##type>(                 \
     697             :         [=](LiftoffRegister dst, LiftoffRegister src) { \
     698             :           __ emit_##fn(dst.fp(), src.fp());             \
     699             :         });                                             \
     700             :     break;
     701             : #define CASE_FLOAT_UNOP_WITH_CFALLBACK(opcode, type, fn)                    \
     702             :   case WasmOpcode::kExpr##opcode:                                           \
     703             :     EmitFloatUnOpWithCFallback<kWasm##type>(&LiftoffAssembler::emit_##fn,   \
     704             :                                             &ExternalReference::wasm_##fn); \
     705             :     break;
     706             : #define CASE_TYPE_CONVERSION(opcode, dst_type, src_type, ext_ref, can_trap) \
     707             :   case WasmOpcode::kExpr##opcode:                                           \
     708             :     EmitTypeConversion<kWasm##dst_type, kWasm##src_type, can_trap>(         \
     709             :         kExpr##opcode, ext_ref, can_trap ? decoder->position() : 0);        \
     710             :     break;
     711      225191 :     switch (opcode) {
     712      209824 :       CASE_I32_UNOP(I32Eqz, i32_eqz)
     713        2288 :       CASE_I32_UNOP(I32Clz, i32_clz)
     714         672 :       CASE_I32_UNOP(I32Ctz, i32_ctz)
     715          64 :       CASE_FLOAT_UNOP(F32Abs, F32, f32_abs)
     716         454 :       CASE_FLOAT_UNOP(F32Neg, F32, f32_neg)
     717          20 :       CASE_FLOAT_UNOP_WITH_CFALLBACK(F32Ceil, F32, f32_ceil)
     718          20 :       CASE_FLOAT_UNOP_WITH_CFALLBACK(F32Floor, F32, f32_floor)
     719          20 :       CASE_FLOAT_UNOP_WITH_CFALLBACK(F32Trunc, F32, f32_trunc)
     720          20 :       CASE_FLOAT_UNOP_WITH_CFALLBACK(F32NearestInt, F32, f32_nearest_int)
     721         608 :       CASE_FLOAT_UNOP(F32Sqrt, F32, f32_sqrt)
     722          64 :       CASE_FLOAT_UNOP(F64Abs, F64, f64_abs)
     723         455 :       CASE_FLOAT_UNOP(F64Neg, F64, f64_neg)
     724          20 :       CASE_FLOAT_UNOP_WITH_CFALLBACK(F64Ceil, F64, f64_ceil)
     725          20 :       CASE_FLOAT_UNOP_WITH_CFALLBACK(F64Floor, F64, f64_floor)
     726          20 :       CASE_FLOAT_UNOP_WITH_CFALLBACK(F64Trunc, F64, f64_trunc)
     727          20 :       CASE_FLOAT_UNOP_WITH_CFALLBACK(F64NearestInt, F64, f64_nearest_int)
     728         512 :       CASE_FLOAT_UNOP(F64Sqrt, F64, f64_sqrt)
     729         760 :       CASE_TYPE_CONVERSION(I32ConvertI64, I32, I64, nullptr, kNoTrap)
     730         112 :       CASE_TYPE_CONVERSION(I32SConvertF32, I32, F32, nullptr, kCanTrap)
     731          44 :       CASE_TYPE_CONVERSION(I32UConvertF32, I32, F32, nullptr, kCanTrap)
     732         112 :       CASE_TYPE_CONVERSION(I32SConvertF64, I32, F64, nullptr, kCanTrap)
     733          36 :       CASE_TYPE_CONVERSION(I32UConvertF64, I32, F64, nullptr, kCanTrap)
     734       50920 :       CASE_TYPE_CONVERSION(I32ReinterpretF32, I32, F32, nullptr, kNoTrap)
     735          72 :       CASE_TYPE_CONVERSION(I64SConvertI32, I64, I32, nullptr, kNoTrap)
     736       12949 :       CASE_TYPE_CONVERSION(I64UConvertI32, I64, I32, nullptr, kNoTrap)
     737          36 :       CASE_TYPE_CONVERSION(I64SConvertF32, I64, F32,
     738             :                            &ExternalReference::wasm_float32_to_int64, kCanTrap)
     739          36 :       CASE_TYPE_CONVERSION(I64UConvertF32, I64, F32,
     740             :                            &ExternalReference::wasm_float32_to_uint64, kCanTrap)
     741         641 :       CASE_TYPE_CONVERSION(I64SConvertF64, I64, F64,
     742             :                            &ExternalReference::wasm_float64_to_int64, kCanTrap)
     743          44 :       CASE_TYPE_CONVERSION(I64UConvertF64, I64, F64,
     744             :                            &ExternalReference::wasm_float64_to_uint64, kCanTrap)
     745       50608 :       CASE_TYPE_CONVERSION(I64ReinterpretF64, I64, F64, nullptr, kNoTrap)
     746          72 :       CASE_TYPE_CONVERSION(F32SConvertI32, F32, I32, nullptr, kNoTrap)
     747          40 :       CASE_TYPE_CONVERSION(F32UConvertI32, F32, I32, nullptr, kNoTrap)
     748          44 :       CASE_TYPE_CONVERSION(F32SConvertI64, F32, I64,
     749             :                            &ExternalReference::wasm_int64_to_float32, kNoTrap)
     750          28 :       CASE_TYPE_CONVERSION(F32UConvertI64, F32, I64,
     751             :                            &ExternalReference::wasm_uint64_to_float32, kNoTrap)
     752          88 :       CASE_TYPE_CONVERSION(F32ConvertF64, F32, F64, nullptr, kNoTrap)
     753         131 :       CASE_TYPE_CONVERSION(F32ReinterpretI32, F32, I32, nullptr, kNoTrap)
     754          60 :       CASE_TYPE_CONVERSION(F64SConvertI32, F64, I32, nullptr, kNoTrap)
     755          64 :       CASE_TYPE_CONVERSION(F64UConvertI32, F64, I32, nullptr, kNoTrap)
     756          64 :       CASE_TYPE_CONVERSION(F64SConvertI64, F64, I64,
     757             :                            &ExternalReference::wasm_int64_to_float64, kNoTrap)
     758         132 :       CASE_TYPE_CONVERSION(F64UConvertI64, F64, I64,
     759             :                            &ExternalReference::wasm_uint64_to_float64, kNoTrap)
     760         152 :       CASE_TYPE_CONVERSION(F64ConvertF32, F64, F32, nullptr, kNoTrap)
     761         172 :       CASE_TYPE_CONVERSION(F64ReinterpretI64, F64, I64, nullptr, kNoTrap)
     762          24 :       CASE_I32_SIGN_EXTENSION(I32SExtendI8, i32_signextend_i8)
     763           8 :       CASE_I32_SIGN_EXTENSION(I32SExtendI16, i32_signextend_i16)
     764          24 :       CASE_I64_SIGN_EXTENSION(I64SExtendI8, i64_signextend_i8)
     765           8 :       CASE_I64_SIGN_EXTENSION(I64SExtendI16, i64_signextend_i16)
     766           8 :       CASE_I64_SIGN_EXTENSION(I64SExtendI32, i64_signextend_i32)
     767             :       case kExprI32Popcnt:
     768             :         EmitI32UnOpWithCFallback(&LiftoffAssembler::emit_i32_popcnt,
     769          76 :                                  &ExternalReference::wasm_word32_popcnt);
     770             :         break;
     771             :       case WasmOpcode::kExprI64Eqz:
     772             :         EmitUnOp<kWasmI64, kWasmI32>(
     773             :             [=](LiftoffRegister dst, LiftoffRegister src) {
     774         164 :               __ emit_i64_eqz(dst.gp(), src);
     775         164 :             });
     776             :         break;
     777             :       default:
     778      225341 :         return unsupported(decoder, WasmOpcodes::OpcodeName(opcode));
     779             :     }
     780             : #undef CASE_I32_UNOP
     781             : #undef CASE_I32_SIGN_EXTENSION
     782             : #undef CASE_I64_SIGN_EXTENSION
     783             : #undef CASE_FLOAT_UNOP
     784             : #undef CASE_FLOAT_UNOP_WITH_CFALLBACK
     785             : #undef CASE_TYPE_CONVERSION
     786             :   }
     787             : 
     788             :   template <ValueType src_type, ValueType result_type, typename EmitFn>
     789      764122 :   void EmitBinOp(EmitFn fn) {
     790             :     static constexpr RegClass src_rc = reg_class_for(src_type);
     791             :     static constexpr RegClass result_rc = reg_class_for(result_type);
     792      764122 :     LiftoffRegister rhs = __ PopToRegister();
     793      764139 :     LiftoffRegister lhs = __ PopToRegister(LiftoffRegList::ForRegs(rhs));
     794             :     LiftoffRegister dst = src_rc == result_rc
     795     1525982 :                               ? __ GetUnusedRegister(result_rc, {lhs, rhs})
     796      762993 :                               : __ GetUnusedRegister(result_rc);
     797      133632 :     fn(dst, lhs, rhs);
     798             :     __ PushRegister(result_type, dst);
     799      764141 :   }
     800             : 
     801             :   void EmitDivOrRem64CCall(LiftoffRegister dst, LiftoffRegister lhs,
     802             :                            LiftoffRegister rhs, ExternalReference ext_ref,
     803             :                            Label* trap_by_zero,
     804             :                            Label* trap_unrepresentable = nullptr) {
     805             :     // Cannot emit native instructions, build C call.
     806             :     LiftoffRegister ret =
     807             :         __ GetUnusedRegister(kGpReg, LiftoffRegList::ForRegs(dst));
     808             :     LiftoffRegister tmp =
     809             :         __ GetUnusedRegister(kGpReg, LiftoffRegList::ForRegs(dst, ret));
     810             :     LiftoffRegister arg_regs[] = {lhs, rhs};
     811             :     LiftoffRegister result_regs[] = {ret, dst};
     812             :     ValueType sig_types[] = {kWasmI32, kWasmI64, kWasmI64};
     813             :     // <i64, i64> -> i32 (with i64 output argument)
     814             :     FunctionSig sig(1, 2, sig_types);
     815             :     GenerateCCall(result_regs, &sig, kWasmI64, arg_regs, ext_ref);
     816             :     __ LoadConstant(tmp, WasmValue(int32_t{0}));
     817             :     __ emit_cond_jump(kEqual, trap_by_zero, kWasmI32, ret.gp(), tmp.gp());
     818             :     if (trap_unrepresentable) {
     819             :       __ LoadConstant(tmp, WasmValue(int32_t{-1}));
     820             :       __ emit_cond_jump(kEqual, trap_unrepresentable, kWasmI32, ret.gp(),
     821             :                         tmp.gp());
     822             :     }
     823             :   }
     824             : 
     825      764302 :   void BinOp(FullDecoder* decoder, WasmOpcode opcode, const Value& lhs,
     826             :              const Value& rhs, Value* result) {
     827             : #define CASE_I32_BINOP(opcode, fn)                                           \
     828             :   case WasmOpcode::kExpr##opcode:                                            \
     829             :     return EmitBinOp<kWasmI32, kWasmI32>(                                    \
     830             :         [=](LiftoffRegister dst, LiftoffRegister lhs, LiftoffRegister rhs) { \
     831             :           __ emit_##fn(dst.gp(), lhs.gp(), rhs.gp());                        \
     832             :         });
     833             : #define CASE_I64_BINOP(opcode, fn)                                           \
     834             :   case WasmOpcode::kExpr##opcode:                                            \
     835             :     return EmitBinOp<kWasmI64, kWasmI64>(                                    \
     836             :         [=](LiftoffRegister dst, LiftoffRegister lhs, LiftoffRegister rhs) { \
     837             :           __ emit_##fn(dst, lhs, rhs);                                       \
     838             :         });
     839             : #define CASE_FLOAT_BINOP(opcode, type, fn)                                   \
     840             :   case WasmOpcode::kExpr##opcode:                                            \
     841             :     return EmitBinOp<kWasm##type, kWasm##type>(                              \
     842             :         [=](LiftoffRegister dst, LiftoffRegister lhs, LiftoffRegister rhs) { \
     843             :           __ emit_##fn(dst.fp(), lhs.fp(), rhs.fp());                        \
     844             :         });
     845             : #define CASE_I32_CMPOP(opcode, cond)                                         \
     846             :   case WasmOpcode::kExpr##opcode:                                            \
     847             :     return EmitBinOp<kWasmI32, kWasmI32>(                                    \
     848             :         [=](LiftoffRegister dst, LiftoffRegister lhs, LiftoffRegister rhs) { \
     849             :           __ emit_i32_set_cond(cond, dst.gp(), lhs.gp(), rhs.gp());          \
     850             :         });
     851             : #define CASE_I64_CMPOP(opcode, cond)                                         \
     852             :   case WasmOpcode::kExpr##opcode:                                            \
     853             :     return EmitBinOp<kWasmI64, kWasmI32>(                                    \
     854             :         [=](LiftoffRegister dst, LiftoffRegister lhs, LiftoffRegister rhs) { \
     855             :           __ emit_i64_set_cond(cond, dst.gp(), lhs, rhs);                    \
     856             :         });
     857             : #define CASE_F32_CMPOP(opcode, cond)                                         \
     858             :   case WasmOpcode::kExpr##opcode:                                            \
     859             :     return EmitBinOp<kWasmF32, kWasmI32>(                                    \
     860             :         [=](LiftoffRegister dst, LiftoffRegister lhs, LiftoffRegister rhs) { \
     861             :           __ emit_f32_set_cond(cond, dst.gp(), lhs.fp(), rhs.fp());          \
     862             :         });
     863             : #define CASE_F64_CMPOP(opcode, cond)                                         \
     864             :   case WasmOpcode::kExpr##opcode:                                            \
     865             :     return EmitBinOp<kWasmF64, kWasmI32>(                                    \
     866             :         [=](LiftoffRegister dst, LiftoffRegister lhs, LiftoffRegister rhs) { \
     867             :           __ emit_f64_set_cond(cond, dst.gp(), lhs.fp(), rhs.fp());          \
     868             :         });
     869             : #define CASE_I32_SHIFTOP(opcode, fn)                                         \
     870             :   case WasmOpcode::kExpr##opcode:                                            \
     871             :     return EmitBinOp<kWasmI32, kWasmI32>(                                    \
     872             :         [=](LiftoffRegister dst, LiftoffRegister lhs, LiftoffRegister rhs) { \
     873             :           __ emit_##fn(dst.gp(), lhs.gp(), rhs.gp(), {});                    \
     874             :         });
     875             : #define CASE_I64_SHIFTOP(opcode, fn)                                           \
     876             :   case WasmOpcode::kExpr##opcode:                                              \
     877             :     return EmitBinOp<kWasmI64, kWasmI64>([=](LiftoffRegister dst,              \
     878             :                                              LiftoffRegister src,              \
     879             :                                              LiftoffRegister amount) {         \
     880             :       __ emit_##fn(dst, src, amount.is_pair() ? amount.low_gp() : amount.gp(), \
     881             :                    {});                                                        \
     882             :     });
     883             : #define CASE_CCALL_BINOP(opcode, type, ext_ref_fn)                           \
     884             :   case WasmOpcode::kExpr##opcode:                                            \
     885             :     return EmitBinOp<kWasmI32, kWasmI32>(                                    \
     886             :         [=](LiftoffRegister dst, LiftoffRegister lhs, LiftoffRegister rhs) { \
     887             :           LiftoffRegister args[] = {lhs, rhs};                               \
     888             :           auto ext_ref = ExternalReference::ext_ref_fn();                    \
     889             :           ValueType sig_i_ii_reps[] = {kWasmI32, kWasmI32, kWasmI32};        \
     890             :           FunctionSig sig_i_ii(1, 2, sig_i_ii_reps);                         \
     891             :           GenerateCCall(&dst, &sig_i_ii, kWasmStmt, args, ext_ref);          \
     892             :         });
     893      764302 :     switch (opcode) {
     894      556184 :       CASE_I32_BINOP(I32Add, i32_add)
     895       48169 :       CASE_I32_BINOP(I32Sub, i32_sub)
     896       28790 :       CASE_I32_BINOP(I32Mul, i32_mul)
     897       42781 :       CASE_I32_BINOP(I32And, i32_and)
     898       27222 :       CASE_I32_BINOP(I32Ior, i32_or)
     899       26952 :       CASE_I32_BINOP(I32Xor, i32_xor)
     900       15352 :       CASE_I64_BINOP(I64And, i64_and)
     901       12979 :       CASE_I64_BINOP(I64Ior, i64_or)
     902         104 :       CASE_I64_BINOP(I64Xor, i64_xor)
     903      167942 :       CASE_I32_CMPOP(I32Eq, kEqual)
     904       27160 :       CASE_I32_CMPOP(I32Ne, kUnequal)
     905       27002 :       CASE_I32_CMPOP(I32LtS, kSignedLessThan)
     906       27112 :       CASE_I32_CMPOP(I32LtU, kUnsignedLessThan)
     907       26984 :       CASE_I32_CMPOP(I32GtS, kSignedGreaterThan)
     908       26984 :       CASE_I32_CMPOP(I32GtU, kUnsignedGreaterThan)
     909       27032 :       CASE_I32_CMPOP(I32LeS, kSignedLessEqual)
     910       27032 :       CASE_I32_CMPOP(I32LeU, kUnsignedLessEqual)
     911       26968 :       CASE_I32_CMPOP(I32GeS, kSignedGreaterEqual)
     912       27008 :       CASE_I32_CMPOP(I32GeU, kUnsignedGreaterEqual)
     913        1358 :       CASE_I64_BINOP(I64Add, i64_add)
     914        1568 :       CASE_I64_BINOP(I64Sub, i64_sub)
     915        1543 :       CASE_I64_BINOP(I64Mul, i64_mul)
     916       69183 :       CASE_I64_CMPOP(I64Eq, kEqual)
     917          56 :       CASE_I64_CMPOP(I64Ne, kUnequal)
     918         120 :       CASE_I64_CMPOP(I64LtS, kSignedLessThan)
     919          72 :       CASE_I64_CMPOP(I64LtU, kUnsignedLessThan)
     920          72 :       CASE_I64_CMPOP(I64GtS, kSignedGreaterThan)
     921          72 :       CASE_I64_CMPOP(I64GtU, kUnsignedGreaterThan)
     922          56 :       CASE_I64_CMPOP(I64LeS, kSignedLessEqual)
     923          88 :       CASE_I64_CMPOP(I64LeU, kUnsignedLessEqual)
     924          56 :       CASE_I64_CMPOP(I64GeS, kSignedGreaterEqual)
     925          56 :       CASE_I64_CMPOP(I64GeU, kUnsignedGreaterEqual)
     926         544 :       CASE_F32_CMPOP(F32Eq, kEqual)
     927         160 :       CASE_F32_CMPOP(F32Ne, kUnequal)
     928         448 :       CASE_F32_CMPOP(F32Lt, kUnsignedLessThan)
     929         480 :       CASE_F32_CMPOP(F32Gt, kUnsignedGreaterThan)
     930         448 :       CASE_F32_CMPOP(F32Le, kUnsignedLessEqual)
     931         416 :       CASE_F32_CMPOP(F32Ge, kUnsignedGreaterEqual)
     932         320 :       CASE_F64_CMPOP(F64Eq, kEqual)
     933         160 :       CASE_F64_CMPOP(F64Ne, kUnequal)
     934         480 :       CASE_F64_CMPOP(F64Lt, kUnsignedLessThan)
     935         384 :       CASE_F64_CMPOP(F64Gt, kUnsignedGreaterThan)
     936         448 :       CASE_F64_CMPOP(F64Le, kUnsignedLessEqual)
     937         384 :       CASE_F64_CMPOP(F64Ge, kUnsignedGreaterEqual)
     938       56400 :       CASE_I32_SHIFTOP(I32Shl, i32_shl)
     939       56304 :       CASE_I32_SHIFTOP(I32ShrS, i32_sar)
     940       56304 :       CASE_I32_SHIFTOP(I32ShrU, i32_shr)
     941       21310 :       CASE_I64_SHIFTOP(I64Shl, i64_shl)
     942        1896 :       CASE_I64_SHIFTOP(I64ShrS, i64_sar)
     943        2280 :       CASE_I64_SHIFTOP(I64ShrU, i64_shr)
     944       40452 :       CASE_CCALL_BINOP(I32Rol, I32, wasm_word32_rol)
     945       40404 :       CASE_CCALL_BINOP(I32Ror, I32, wasm_word32_ror)
     946        1356 :       CASE_FLOAT_BINOP(F32Add, F32, f32_add)
     947        1080 :       CASE_FLOAT_BINOP(F32Sub, F32, f32_sub)
     948        1341 :       CASE_FLOAT_BINOP(F32Mul, F32, f32_mul)
     949        1137 :       CASE_FLOAT_BINOP(F32Div, F32, f32_div)
     950         128 :       CASE_FLOAT_BINOP(F32Min, F32, f32_min)
     951         128 :       CASE_FLOAT_BINOP(F32Max, F32, f32_max)
     952          84 :       CASE_FLOAT_BINOP(F32CopySign, F32, f32_copysign)
     953        2500 :       CASE_FLOAT_BINOP(F64Add, F64, f64_add)
     954        1092 :       CASE_FLOAT_BINOP(F64Sub, F64, f64_sub)
     955        1685 :       CASE_FLOAT_BINOP(F64Mul, F64, f64_mul)
     956        1320 :       CASE_FLOAT_BINOP(F64Div, F64, f64_div)
     957         160 :       CASE_FLOAT_BINOP(F64Min, F64, f64_min)
     958         128 :       CASE_FLOAT_BINOP(F64Max, F64, f64_max)
     959          84 :       CASE_FLOAT_BINOP(F64CopySign, F64, f64_copysign)
     960             :       case WasmOpcode::kExprI32DivS:
     961             :         EmitBinOp<kWasmI32, kWasmI32>([this, decoder](LiftoffRegister dst,
     962             :                                                       LiftoffRegister lhs,
     963       14192 :                                                       LiftoffRegister rhs) {
     964       14192 :           WasmCodePosition position = decoder->position();
     965       14192 :           AddOutOfLineTrap(position, WasmCode::kThrowWasmTrapDivByZero);
     966             :           // Adding the second trap might invalidate the pointer returned for
     967             :           // the first one, thus get both pointers afterwards.
     968             :           AddOutOfLineTrap(position,
     969       14192 :                            WasmCode::kThrowWasmTrapDivUnrepresentable);
     970       28384 :           Label* div_by_zero = out_of_line_code_.end()[-2].label.get();
     971       14192 :           Label* div_unrepresentable = out_of_line_code_.end()[-1].label.get();
     972             :           __ emit_i32_divs(dst.gp(), lhs.gp(), rhs.gp(), div_by_zero,
     973       14192 :                            div_unrepresentable);
     974       28384 :         });
     975             :         break;
     976             :       case WasmOpcode::kExprI32DivU:
     977             :         EmitBinOp<kWasmI32, kWasmI32>([this, decoder](LiftoffRegister dst,
     978             :                                                       LiftoffRegister lhs,
     979       14172 :                                                       LiftoffRegister rhs) {
     980             :           Label* div_by_zero = AddOutOfLineTrap(
     981       28344 :               decoder->position(), WasmCode::kThrowWasmTrapDivByZero);
     982       14172 :           __ emit_i32_divu(dst.gp(), lhs.gp(), rhs.gp(), div_by_zero);
     983       28344 :         });
     984             :         break;
     985             :       case WasmOpcode::kExprI32RemS:
     986             :         EmitBinOp<kWasmI32, kWasmI32>([this, decoder](LiftoffRegister dst,
     987             :                                                       LiftoffRegister lhs,
     988       14108 :                                                       LiftoffRegister rhs) {
     989             :           Label* rem_by_zero = AddOutOfLineTrap(
     990       28216 :               decoder->position(), WasmCode::kThrowWasmTrapRemByZero);
     991       14108 :           __ emit_i32_rems(dst.gp(), lhs.gp(), rhs.gp(), rem_by_zero);
     992       28216 :         });
     993             :         break;
     994             :       case WasmOpcode::kExprI32RemU:
     995             :         EmitBinOp<kWasmI32, kWasmI32>([this, decoder](LiftoffRegister dst,
     996             :                                                       LiftoffRegister lhs,
     997       14104 :                                                       LiftoffRegister rhs) {
     998             :           Label* rem_by_zero = AddOutOfLineTrap(
     999       28208 :               decoder->position(), WasmCode::kThrowWasmTrapRemByZero);
    1000       14104 :           __ emit_i32_remu(dst.gp(), lhs.gp(), rhs.gp(), rem_by_zero);
    1001       28208 :         });
    1002             :         break;
    1003             :       case WasmOpcode::kExprI64DivS:
    1004             :         EmitBinOp<kWasmI64, kWasmI64>([this, decoder](LiftoffRegister dst,
    1005             :                                                       LiftoffRegister lhs,
    1006         716 :                                                       LiftoffRegister rhs) {
    1007         716 :           WasmCodePosition position = decoder->position();
    1008         716 :           AddOutOfLineTrap(position, WasmCode::kThrowWasmTrapDivByZero);
    1009             :           // Adding the second trap might invalidate the pointer returned for
    1010             :           // the first one, thus get both pointers afterwards.
    1011             :           AddOutOfLineTrap(position,
    1012         716 :                            WasmCode::kThrowWasmTrapDivUnrepresentable);
    1013        1432 :           Label* div_by_zero = out_of_line_code_.end()[-2].label.get();
    1014         716 :           Label* div_unrepresentable = out_of_line_code_.end()[-1].label.get();
    1015             :           if (!__ emit_i64_divs(dst, lhs, rhs, div_by_zero,
    1016         716 :                                 div_unrepresentable)) {
    1017             :             ExternalReference ext_ref = ExternalReference::wasm_int64_div();
    1018             :             EmitDivOrRem64CCall(dst, lhs, rhs, ext_ref, div_by_zero,
    1019             :                                 div_unrepresentable);
    1020             :           }
    1021        1432 :         });
    1022             :         break;
    1023             :       case WasmOpcode::kExprI64DivU:
    1024             :         EmitBinOp<kWasmI64, kWasmI64>([this, decoder](LiftoffRegister dst,
    1025             :                                                       LiftoffRegister lhs,
    1026         656 :                                                       LiftoffRegister rhs) {
    1027             :           Label* div_by_zero = AddOutOfLineTrap(
    1028        1312 :               decoder->position(), WasmCode::kThrowWasmTrapDivByZero);
    1029         656 :           if (!__ emit_i64_divu(dst, lhs, rhs, div_by_zero)) {
    1030             :             ExternalReference ext_ref = ExternalReference::wasm_uint64_div();
    1031             :             EmitDivOrRem64CCall(dst, lhs, rhs, ext_ref, div_by_zero);
    1032             :           }
    1033        1312 :         });
    1034             :         break;
    1035             :       case WasmOpcode::kExprI64RemS:
    1036             :         EmitBinOp<kWasmI64, kWasmI64>([this, decoder](LiftoffRegister dst,
    1037             :                                                       LiftoffRegister lhs,
    1038         640 :                                                       LiftoffRegister rhs) {
    1039             :           Label* rem_by_zero = AddOutOfLineTrap(
    1040        1280 :               decoder->position(), WasmCode::kThrowWasmTrapRemByZero);
    1041         640 :           if (!__ emit_i64_rems(dst, lhs, rhs, rem_by_zero)) {
    1042             :             ExternalReference ext_ref = ExternalReference::wasm_int64_mod();
    1043             :             EmitDivOrRem64CCall(dst, lhs, rhs, ext_ref, rem_by_zero);
    1044             :           }
    1045        1280 :         });
    1046             :         break;
    1047             :       case WasmOpcode::kExprI64RemU:
    1048             :         EmitBinOp<kWasmI64, kWasmI64>([this, decoder](LiftoffRegister dst,
    1049             :                                                       LiftoffRegister lhs,
    1050         640 :                                                       LiftoffRegister rhs) {
    1051             :           Label* rem_by_zero = AddOutOfLineTrap(
    1052        1280 :               decoder->position(), WasmCode::kThrowWasmTrapRemByZero);
    1053         640 :           if (!__ emit_i64_remu(dst, lhs, rhs, rem_by_zero)) {
    1054             :             ExternalReference ext_ref = ExternalReference::wasm_uint64_mod();
    1055             :             EmitDivOrRem64CCall(dst, lhs, rhs, ext_ref, rem_by_zero);
    1056             :           }
    1057        1280 :         });
    1058             :         break;
    1059             :       default:
    1060         156 :         return unsupported(decoder, WasmOpcodes::OpcodeName(opcode));
    1061             :     }
    1062             : #undef CASE_I32_BINOP
    1063             : #undef CASE_I64_BINOP
    1064             : #undef CASE_FLOAT_BINOP
    1065             : #undef CASE_I32_CMPOP
    1066             : #undef CASE_I64_CMPOP
    1067             : #undef CASE_F32_CMPOP
    1068             : #undef CASE_F64_CMPOP
    1069             : #undef CASE_I32_SHIFTOP
    1070             : #undef CASE_I64_SHIFTOP
    1071             : #undef CASE_CCALL_BINOP
    1072             :   }
    1073             : 
    1074             :   void I32Const(FullDecoder* decoder, Value* result, int32_t value) {
    1075     1307578 :     __ cache_state()->stack_state.emplace_back(kWasmI32, value);
    1076             :   }
    1077             : 
    1078       41587 :   void I64Const(FullDecoder* decoder, Value* result, int64_t value) {
    1079             :     // The {VarState} stores constant values as int32_t, thus we only store
    1080             :     // 64-bit constants in this field if it fits in an int32_t. Larger values
    1081             :     // cannot be used as immediate value anyway, so we can also just put them in
    1082             :     // a register immediately.
    1083       41587 :     int32_t value_i32 = static_cast<int32_t>(value);
    1084       41587 :     if (value_i32 == value) {
    1085       20171 :       __ cache_state()->stack_state.emplace_back(kWasmI64, value_i32);
    1086             :     } else {
    1087       21416 :       LiftoffRegister reg = __ GetUnusedRegister(reg_class_for(kWasmI64));
    1088       21416 :       __ LoadConstant(reg, WasmValue(value));
    1089             :       __ PushRegister(kWasmI64, reg);
    1090             :     }
    1091       41590 :   }
    1092             : 
    1093      118148 :   void F32Const(FullDecoder* decoder, Value* result, float value) {
    1094      118148 :     LiftoffRegister reg = __ GetUnusedRegister(kFpReg);
    1095      118148 :     __ LoadConstant(reg, WasmValue(value));
    1096             :     __ PushRegister(kWasmF32, reg);
    1097      118150 :   }
    1098             : 
    1099      119014 :   void F64Const(FullDecoder* decoder, Value* result, double value) {
    1100      119014 :     LiftoffRegister reg = __ GetUnusedRegister(kFpReg);
    1101      119014 :     __ LoadConstant(reg, WasmValue(value));
    1102             :     __ PushRegister(kWasmF64, reg);
    1103      119015 :   }
    1104             : 
    1105          64 :   void RefNull(FullDecoder* decoder, Value* result) {
    1106             :     unsupported(decoder, "ref_null");
    1107          64 :   }
    1108             : 
    1109        2916 :   void Drop(FullDecoder* decoder, const Value& value) {
    1110        2916 :     auto& slot = __ cache_state()->stack_state.back();
    1111             :     // If the dropped slot contains a register, decrement it's use count.
    1112        2916 :     if (slot.is_reg()) __ cache_state()->dec_used(slot.reg());
    1113             :     __ cache_state()->stack_state.pop_back();
    1114        2916 :   }
    1115             : 
    1116      576143 :   void ReturnImpl(FullDecoder* decoder) {
    1117      576143 :     size_t num_returns = decoder->sig_->return_count();
    1118     1152316 :     if (num_returns > 1) return unsupported(decoder, "multi-return");
    1119      575583 :     if (num_returns > 0) __ MoveToReturnRegisters(decoder->sig_);
    1120      575586 :     __ LeaveFrame(StackFrame::WASM_COMPILED);
    1121             :     __ DropStackSlotsAndRet(
    1122      575596 :         static_cast<uint32_t>(descriptor_->StackParameterCount()));
    1123             :   }
    1124             : 
    1125             :   void DoReturn(FullDecoder* decoder, Vector<Value> /*values*/) {
    1126      576014 :     ReturnImpl(decoder);
    1127             :   }
    1128             : 
    1129      250006 :   void GetLocal(FullDecoder* decoder, Value* result,
    1130             :                 const LocalIndexImmediate<validate>& imm) {
    1131      750034 :     auto& slot = __ cache_state()->stack_state[imm.index];
    1132             :     DCHECK_EQ(slot.type(), imm.type);
    1133      250006 :     switch (slot.loc()) {
    1134             :       case kRegister:
    1135             :         __ PushRegister(slot.type(), slot.reg());
    1136             :         break;
    1137             :       case kIntConst:
    1138         420 :         __ cache_state()->stack_state.emplace_back(imm.type, slot.i32_const());
    1139             :         break;
    1140             :       case kStack: {
    1141       72033 :         auto rc = reg_class_for(imm.type);
    1142       72033 :         LiftoffRegister reg = __ GetUnusedRegister(rc);
    1143       72033 :         __ Fill(reg, imm.index, imm.type);
    1144             :         __ PushRegister(slot.type(), reg);
    1145             :         break;
    1146             :       }
    1147             :     }
    1148      250003 :   }
    1149             : 
    1150           0 :   void SetLocalFromStackSlot(LiftoffAssembler::VarState& dst_slot,
    1151             :                              uint32_t local_index) {
    1152             :     auto& state = *__ cache_state();
    1153             :     ValueType type = dst_slot.type();
    1154           0 :     if (dst_slot.is_reg()) {
    1155             :       LiftoffRegister slot_reg = dst_slot.reg();
    1156           0 :       if (state.get_use_count(slot_reg) == 1) {
    1157           0 :         __ Fill(dst_slot.reg(), state.stack_height() - 1, type);
    1158           0 :         return;
    1159             :       }
    1160             :       state.dec_used(slot_reg);
    1161             :       dst_slot.MakeStack();
    1162             :     }
    1163             :     DCHECK_EQ(type, __ local_type(local_index));
    1164             :     RegClass rc = reg_class_for(type);
    1165           0 :     LiftoffRegister dst_reg = __ GetUnusedRegister(rc);
    1166           0 :     __ Fill(dst_reg, __ cache_state()->stack_height() - 1, type);
    1167           0 :     dst_slot = LiftoffAssembler::VarState(type, dst_reg);
    1168             :     __ cache_state()->inc_used(dst_reg);
    1169             :   }
    1170             : 
    1171       76663 :   void SetLocal(uint32_t local_index, bool is_tee) {
    1172             :     auto& state = *__ cache_state();
    1173      229989 :     auto& source_slot = state.stack_state.back();
    1174      153327 :     auto& target_slot = state.stack_state[local_index];
    1175       76663 :     switch (source_slot.loc()) {
    1176             :       case kRegister:
    1177       75342 :         if (target_slot.is_reg()) state.dec_used(target_slot.reg());
    1178       75342 :         target_slot = source_slot;
    1179       75342 :         if (is_tee) state.inc_used(target_slot.reg());
    1180             :         break;
    1181             :       case kIntConst:
    1182        1322 :         if (target_slot.is_reg()) state.dec_used(target_slot.reg());
    1183        1322 :         target_slot = source_slot;
    1184        1322 :         break;
    1185             :       case kStack:
    1186           0 :         SetLocalFromStackSlot(target_slot, local_index);
    1187           0 :         break;
    1188             :     }
    1189       76663 :     if (!is_tee) __ cache_state()->stack_state.pop_back();
    1190       76663 :   }
    1191             : 
    1192             :   void SetLocal(FullDecoder* decoder, const Value& value,
    1193             :                 const LocalIndexImmediate<validate>& imm) {
    1194       75601 :     SetLocal(imm.index, false);
    1195             :   }
    1196             : 
    1197             :   void TeeLocal(FullDecoder* decoder, const Value& value, Value* result,
    1198             :                 const LocalIndexImmediate<validate>& imm) {
    1199        1063 :     SetLocal(imm.index, true);
    1200             :   }
    1201             : 
    1202        1530 :   Register GetGlobalBaseAndOffset(const WasmGlobal* global,
    1203             :                                   LiftoffRegList& pinned, uint32_t* offset) {
    1204        1530 :     Register addr = pinned.set(__ GetUnusedRegister(kGpReg)).gp();
    1205        1530 :     if (global->mutability && global->imported) {
    1206         168 :       LOAD_INSTANCE_FIELD(addr, ImportedMutableGlobals, kSystemPointerSize);
    1207             :       __ Load(LiftoffRegister(addr), addr, no_reg,
    1208         336 :               global->index * sizeof(Address), kPointerLoadType, pinned);
    1209         167 :       *offset = 0;
    1210             :     } else {
    1211        1362 :       LOAD_INSTANCE_FIELD(addr, GlobalsStart, kSystemPointerSize);
    1212        1364 :       *offset = global->offset;
    1213             :     }
    1214        1531 :     return addr;
    1215             :   }
    1216             : 
    1217        1255 :   void GetGlobal(FullDecoder* decoder, Value* result,
    1218             :                  const GlobalIndexImmediate<validate>& imm) {
    1219        1255 :     const auto* global = &env_->module->globals[imm.index];
    1220        1255 :     if (!CheckSupportedType(decoder, kSupportedTypes, global->type, "global"))
    1221         143 :       return;
    1222        1111 :     LiftoffRegList pinned;
    1223        1111 :     uint32_t offset = 0;
    1224        1111 :     Register addr = GetGlobalBaseAndOffset(global, pinned, &offset);
    1225             :     LiftoffRegister value =
    1226        2224 :         pinned.set(__ GetUnusedRegister(reg_class_for(global->type), pinned));
    1227        1112 :     LoadType type = LoadType::ForValueType(global->type);
    1228        1111 :     __ Load(value, addr, no_reg, offset, type, pinned, nullptr, true);
    1229        1110 :     __ PushRegister(global->type, value);
    1230             :   }
    1231             : 
    1232         420 :   void SetGlobal(FullDecoder* decoder, const Value& value,
    1233             :                  const GlobalIndexImmediate<validate>& imm) {
    1234         420 :     auto* global = &env_->module->globals[imm.index];
    1235         420 :     if (!CheckSupportedType(decoder, kSupportedTypes, global->type, "global"))
    1236           0 :       return;
    1237         420 :     LiftoffRegList pinned;
    1238         420 :     uint32_t offset = 0;
    1239         420 :     Register addr = GetGlobalBaseAndOffset(global, pinned, &offset);
    1240         840 :     LiftoffRegister reg = pinned.set(__ PopToRegister(pinned));
    1241         420 :     StoreType type = StoreType::ForValueType(global->type);
    1242         420 :     __ Store(addr, no_reg, offset, reg, type, {}, nullptr, true);
    1243             :   }
    1244             : 
    1245           0 :   void GetTable(FullDecoder* decoder, const Value& index, Value* result,
    1246             :                 TableIndexImmediate<validate>& imm) {
    1247             :     unsupported(decoder, "table_get");
    1248           0 :   }
    1249             : 
    1250           0 :   void SetTable(FullDecoder* decoder, const Value& index, const Value& value,
    1251             :                 TableIndexImmediate<validate>& imm) {
    1252             :     unsupported(decoder, "table_set");
    1253           0 :   }
    1254             : 
    1255      106085 :   void Unreachable(FullDecoder* decoder) {
    1256             :     Label* unreachable_label = AddOutOfLineTrap(
    1257      106085 :         decoder->position(), WasmCode::kThrowWasmTrapUnreachable);
    1258             :     __ emit_jump(unreachable_label);
    1259             :     __ AssertUnreachable(AbortReason::kUnexpectedReturnFromWasmTrap);
    1260      106085 :   }
    1261             : 
    1262         857 :   void Select(FullDecoder* decoder, const Value& cond, const Value& fval,
    1263             :               const Value& tval, Value* result) {
    1264             :     LiftoffRegList pinned;
    1265        1714 :     Register condition = pinned.set(__ PopToRegister()).gp();
    1266         857 :     ValueType type = __ cache_state()->stack_state.end()[-1].type();
    1267             :     DCHECK_EQ(type, __ cache_state()->stack_state.end()[-2].type());
    1268         857 :     LiftoffRegister false_value = pinned.set(__ PopToRegister(pinned));
    1269         858 :     LiftoffRegister true_value = __ PopToRegister(pinned);
    1270             :     LiftoffRegister dst =
    1271        1720 :         __ GetUnusedRegister(true_value.reg_class(), {true_value, false_value});
    1272             :     __ PushRegister(type, dst);
    1273             : 
    1274             :     // Now emit the actual code to move either {true_value} or {false_value}
    1275             :     // into {dst}.
    1276         860 :     Label cont;
    1277         860 :     Label case_false;
    1278         860 :     __ emit_cond_jump(kEqual, &case_false, kWasmI32, condition);
    1279         857 :     if (dst != true_value) __ Move(dst, true_value, type);
    1280             :     __ emit_jump(&cont);
    1281             : 
    1282         858 :     __ bind(&case_false);
    1283         858 :     if (dst != false_value) __ Move(dst, false_value, type);
    1284         859 :     __ bind(&cont);
    1285         859 :   }
    1286             : 
    1287      121379 :   void BrImpl(Control* target) {
    1288      121379 :     if (!target->br_merge()->reached) {
    1289      120299 :       target->label_state.InitMerge(*__ cache_state(), __ num_locals(),
    1290             :                                     target->br_merge()->arity,
    1291      360897 :                                     target->stack_depth);
    1292             :     }
    1293      121380 :     __ MergeStackWith(target->label_state, target->br_merge()->arity);
    1294      121377 :     __ jmp(target->label.get());
    1295      121379 :   }
    1296             : 
    1297        2377 :   void Br(FullDecoder* decoder, Control* target) { BrImpl(target); }
    1298             : 
    1299      119175 :   void BrOrRet(FullDecoder* decoder, uint32_t depth) {
    1300      119175 :     if (depth == decoder->control_depth() - 1) {
    1301         172 :       ReturnImpl(decoder);
    1302             :     } else {
    1303      119003 :       BrImpl(decoder->control_at(depth));
    1304             :     }
    1305      119174 :   }
    1306             : 
    1307      105931 :   void BrIf(FullDecoder* decoder, const Value& cond, uint32_t depth) {
    1308      105931 :     Label cont_false;
    1309      211862 :     Register value = __ PopToRegister().gp();
    1310      105931 :     __ emit_cond_jump(kEqual, &cont_false, kWasmI32, value);
    1311             : 
    1312      105931 :     BrOrRet(decoder, depth);
    1313      105930 :     __ bind(&cont_false);
    1314      105930 :   }
    1315             : 
    1316             :   // Generate a branch table case, potentially reusing previously generated
    1317             :   // stack transfer code.
    1318      216035 :   void GenerateBrCase(FullDecoder* decoder, uint32_t br_depth,
    1319             :                       std::map<uint32_t, MovableLabel>& br_targets) {
    1320      216035 :     MovableLabel& label = br_targets[br_depth];
    1321      216035 :     if (label.get()->is_bound()) {
    1322      202791 :       __ jmp(label.get());
    1323             :     } else {
    1324       13244 :       __ bind(label.get());
    1325       13244 :       BrOrRet(decoder, br_depth);
    1326             :     }
    1327      216036 :   }
    1328             : 
    1329             :   // Generate a branch table for input in [min, max).
    1330             :   // TODO(wasm): Generate a real branch table (like TF TableSwitch).
    1331      416586 :   void GenerateBrTable(FullDecoder* decoder, LiftoffRegister tmp,
    1332             :                        LiftoffRegister value, uint32_t min, uint32_t max,
    1333             :                        BranchTableIterator<validate>& table_iterator,
    1334             :                        std::map<uint32_t, MovableLabel>& br_targets) {
    1335             :     DCHECK_LT(min, max);
    1336             :     // Check base case.
    1337      416586 :     if (max == min + 1) {
    1338             :       DCHECK_EQ(min, table_iterator.cur_index());
    1339      210723 :       GenerateBrCase(decoder, table_iterator.next(), br_targets);
    1340      627310 :       return;
    1341             :     }
    1342             : 
    1343      205863 :     uint32_t split = min + (max - min) / 2;
    1344      205863 :     Label upper_half;
    1345      205863 :     __ LoadConstant(tmp, WasmValue(split));
    1346             :     __ emit_cond_jump(kUnsignedGreaterEqual, &upper_half, kWasmI32, value.gp(),
    1347      205863 :                       tmp.gp());
    1348             :     // Emit br table for lower half:
    1349             :     GenerateBrTable(decoder, tmp, value, min, split, table_iterator,
    1350      205864 :                     br_targets);
    1351      205864 :     __ bind(&upper_half);
    1352             :     // Emit br table for upper half:
    1353             :     GenerateBrTable(decoder, tmp, value, split, max, table_iterator,
    1354      205864 :                     br_targets);
    1355             :   }
    1356             : 
    1357        5312 :   void BrTable(FullDecoder* decoder, const BranchTableImmediate<validate>& imm,
    1358             :                const Value& key) {
    1359             :     LiftoffRegList pinned;
    1360       10623 :     LiftoffRegister value = pinned.set(__ PopToRegister());
    1361             :     BranchTableIterator<validate> table_iterator(decoder, imm);
    1362             :     std::map<uint32_t, MovableLabel> br_targets;
    1363             : 
    1364        5311 :     if (imm.table_count > 0) {
    1365             :       LiftoffRegister tmp = __ GetUnusedRegister(kGpReg, pinned);
    1366        4860 :       __ LoadConstant(tmp, WasmValue(uint32_t{imm.table_count}));
    1367        4859 :       Label case_default;
    1368             :       __ emit_cond_jump(kUnsignedGreaterEqual, &case_default, kWasmI32,
    1369        4859 :                         value.gp(), tmp.gp());
    1370             : 
    1371             :       GenerateBrTable(decoder, tmp, value, 0, imm.table_count, table_iterator,
    1372        4859 :                       br_targets);
    1373             : 
    1374        4860 :       __ bind(&case_default);
    1375             :     }
    1376             : 
    1377             :     // Generate the default case.
    1378        5312 :     GenerateBrCase(decoder, table_iterator.next(), br_targets);
    1379             :     DCHECK(!table_iterator.has_next());
    1380        5312 :   }
    1381             : 
    1382        1632 :   void Else(FullDecoder* decoder, Control* c) {
    1383        1632 :     if (c->reachable()) {
    1384        1373 :       if (!c->end_merge.reached) {
    1385        1369 :         c->label_state.InitMerge(*__ cache_state(), __ num_locals(),
    1386        2738 :                                  c->end_merge.arity, c->stack_depth);
    1387             :       }
    1388        1376 :       __ MergeFullStackWith(c->label_state, *__ cache_state());
    1389        1372 :       __ emit_jump(c->label.get());
    1390             :     }
    1391        1633 :     __ bind(c->else_state->label.get());
    1392        1637 :     __ cache_state()->Steal(c->else_state->state);
    1393        1634 :   }
    1394             : 
    1395      385759 :   Label* AddOutOfLineTrap(WasmCodePosition position,
    1396             :                           WasmCode::RuntimeStubId stub, uint32_t pc = 0) {
    1397             :     DCHECK(!FLAG_wasm_no_bounds_checks);
    1398             :     // The pc is needed for memory OOB trap with trap handler enabled. Other
    1399             :     // callers should not even compute it.
    1400             :     DCHECK_EQ(pc != 0, stub == WasmCode::kThrowWasmTrapMemOutOfBounds &&
    1401             :                            env_->use_trap_handler);
    1402             : 
    1403      771591 :     out_of_line_code_.push_back(OutOfLineCode::Trap(stub, position, pc));
    1404      385832 :     return out_of_line_code_.back().label.get();
    1405             :   }
    1406             : 
    1407             :   // Returns true if the memory access is statically known to be out of bounds
    1408             :   // (a jump to the trap was generated then); return false otherwise.
    1409      200599 :   bool BoundsCheckMem(FullDecoder* decoder, uint32_t access_size,
    1410             :                       uint32_t offset, Register index, LiftoffRegList pinned) {
    1411             :     const bool statically_oob =
    1412      198967 :         !IsInBounds(offset, access_size, env_->max_memory_size);
    1413             : 
    1414      198967 :     if (!statically_oob &&
    1415      197395 :         (FLAG_wasm_no_bounds_checks || env_->use_trap_handler)) {
    1416             :       return false;
    1417             :     }
    1418             : 
    1419             :     // TODO(wasm): This adds protected instruction information for the jump
    1420             :     // instruction we are about to generate. It would be better to just not add
    1421             :     // protected instruction info when the pc is 0.
    1422             :     Label* trap_label = AddOutOfLineTrap(
    1423             :         decoder->position(), WasmCode::kThrowWasmTrapMemOutOfBounds,
    1424        4856 :         env_->use_trap_handler ? __ pc_offset() : 0);
    1425             : 
    1426        1636 :     if (statically_oob) {
    1427             :       __ emit_jump(trap_label);
    1428             :       Control* current_block = decoder->control_at(0);
    1429        1602 :       if (current_block->reachable()) {
    1430        1603 :         current_block->reachability = kSpecOnlyReachable;
    1431             :       }
    1432             :       return true;
    1433             :     }
    1434             : 
    1435             :     DCHECK(!env_->use_trap_handler);
    1436             :     DCHECK(!FLAG_wasm_no_bounds_checks);
    1437             : 
    1438          32 :     uint64_t end_offset = uint64_t{offset} + access_size - 1u;
    1439             : 
    1440             :     // If the end offset is larger than the smallest memory, dynamically check
    1441             :     // the end offset against the actual memory size, which is not known at
    1442             :     // compile time. Otherwise, only one check is required (see below).
    1443             :     LiftoffRegister end_offset_reg =
    1444          32 :         pinned.set(__ GetUnusedRegister(kGpReg, pinned));
    1445          32 :     Register mem_size = __ GetUnusedRegister(kGpReg, pinned).gp();
    1446          32 :     LOAD_INSTANCE_FIELD(mem_size, MemorySize, kSystemPointerSize);
    1447             : 
    1448             :     if (kSystemPointerSize == 8) {
    1449          32 :       __ LoadConstant(end_offset_reg, WasmValue(end_offset));
    1450             :     } else {
    1451             :       __ LoadConstant(end_offset_reg,
    1452             :                       WasmValue(static_cast<uint32_t>(end_offset)));
    1453             :     }
    1454             : 
    1455          32 :     if (end_offset >= env_->min_memory_size) {
    1456             :       __ emit_cond_jump(kUnsignedGreaterEqual, trap_label,
    1457             :                         LiftoffAssembler::kWasmIntPtr, end_offset_reg.gp(),
    1458          15 :                         mem_size);
    1459             :     }
    1460             : 
    1461             :     // Just reuse the end_offset register for computing the effective size.
    1462             :     LiftoffRegister effective_size_reg = end_offset_reg;
    1463          32 :     __ emit_ptrsize_sub(effective_size_reg.gp(), mem_size, end_offset_reg.gp());
    1464             : 
    1465             :     __ emit_i32_to_intptr(index, index);
    1466             : 
    1467             :     __ emit_cond_jump(kUnsignedGreaterEqual, trap_label,
    1468             :                       LiftoffAssembler::kWasmIntPtr, index,
    1469          32 :                       effective_size_reg.gp());
    1470          32 :     return false;
    1471             :   }
    1472             : 
    1473          39 :   void TraceMemoryOperation(bool is_store, MachineRepresentation rep,
    1474             :                             Register index, uint32_t offset,
    1475             :                             WasmCodePosition position) {
    1476             :     // Before making the runtime call, spill all cache registers.
    1477          39 :     __ SpillAllRegisters();
    1478             : 
    1479             :     LiftoffRegList pinned = LiftoffRegList::ForRegs(index);
    1480             :     // Get one register for computing the address (offset + index).
    1481             :     LiftoffRegister address = pinned.set(__ GetUnusedRegister(kGpReg, pinned));
    1482             :     // Compute offset+index in address.
    1483          40 :     __ LoadConstant(address, WasmValue(offset));
    1484          39 :     __ emit_i32_add(address.gp(), address.gp(), index);
    1485             : 
    1486             :     // Get a register to hold the stack slot for MemoryTracingInfo.
    1487             :     LiftoffRegister info = pinned.set(__ GetUnusedRegister(kGpReg, pinned));
    1488             :     // Allocate stack slot for MemoryTracingInfo.
    1489          40 :     __ AllocateStackSlot(info.gp(), sizeof(MemoryTracingInfo));
    1490             : 
    1491             :     // Now store all information into the MemoryTracingInfo struct.
    1492             :     __ Store(info.gp(), no_reg, offsetof(MemoryTracingInfo, address), address,
    1493          39 :              StoreType::kI32Store, pinned);
    1494          40 :     __ LoadConstant(address, WasmValue(is_store ? 1 : 0));
    1495             :     __ Store(info.gp(), no_reg, offsetof(MemoryTracingInfo, is_store), address,
    1496          39 :              StoreType::kI32Store8, pinned);
    1497          40 :     __ LoadConstant(address, WasmValue(static_cast<int>(rep)));
    1498             :     __ Store(info.gp(), no_reg, offsetof(MemoryTracingInfo, mem_rep), address,
    1499          40 :              StoreType::kI32Store8, pinned);
    1500             : 
    1501          40 :     source_position_table_builder_.AddPosition(__ pc_offset(),
    1502          40 :                                                SourcePosition(position), false);
    1503             : 
    1504          40 :     Register args[] = {info.gp()};
    1505          40 :     GenerateRuntimeCall(Runtime::kWasmTraceMemory, arraysize(args), args);
    1506             :     __ DeallocateStackSlot(sizeof(MemoryTracingInfo));
    1507          40 :   }
    1508             : 
    1509          40 :   void GenerateRuntimeCall(Runtime::FunctionId runtime_function, int num_args,
    1510             :                            Register* args) {
    1511             :     auto call_descriptor = compiler::Linkage::GetRuntimeCallDescriptor(
    1512             :         compilation_zone_, runtime_function, num_args,
    1513          40 :         compiler::Operator::kNoProperties, compiler::CallDescriptor::kNoFlags);
    1514             :     // Currently, only one argument is supported. More arguments require some
    1515             :     // caution for the parallel register moves (reuse StackTransferRecipe).
    1516             :     DCHECK_EQ(1, num_args);
    1517             :     constexpr size_t kInputShift = 1;  // Input 0 is the call target.
    1518             :     compiler::LinkageLocation param_loc =
    1519          39 :         call_descriptor->GetInputLocation(kInputShift);
    1520          39 :     if (param_loc.IsRegister()) {
    1521             :       Register reg = Register::from_code(param_loc.AsRegister());
    1522             :       __ Move(LiftoffRegister(reg), LiftoffRegister(args[0]),
    1523           0 :               LiftoffAssembler::kWasmIntPtr);
    1524             :     } else {
    1525             :       DCHECK(param_loc.IsCallerFrameSlot());
    1526          39 :       LiftoffStackSlots stack_slots(&asm_);
    1527             :       stack_slots.Add(LiftoffAssembler::VarState(LiftoffAssembler::kWasmIntPtr,
    1528          39 :                                                  LiftoffRegister(args[0])));
    1529          39 :       stack_slots.Construct();
    1530             :     }
    1531             : 
    1532             :     // Set context to "no context" for the runtime call.
    1533             :     __ TurboAssembler::Move(kContextRegister,
    1534          39 :                             Smi::FromInt(Context::kNoContext));
    1535          40 :     Register centry = kJavaScriptCallCodeStartRegister;
    1536          40 :     LOAD_TAGGED_PTR_INSTANCE_FIELD(centry, CEntryStub);
    1537          40 :     __ CallRuntimeWithCEntry(runtime_function, centry);
    1538             :     safepoint_table_builder_.DefineSafepoint(&asm_, Safepoint::kSimple,
    1539          39 :                                              Safepoint::kNoLazyDeopt);
    1540          40 :   }
    1541             : 
    1542      197371 :   Register AddMemoryMasking(Register index, uint32_t* offset,
    1543             :                             LiftoffRegList& pinned) {
    1544      197371 :     if (!FLAG_untrusted_code_mitigations || env_->use_trap_handler) {
    1545      197371 :       return index;
    1546             :     }
    1547             :     DEBUG_CODE_COMMENT("Mask memory index");
    1548             :     // Make sure that we can overwrite {index}.
    1549           0 :     if (__ cache_state()->is_used(LiftoffRegister(index))) {
    1550             :       Register old_index = index;
    1551             :       pinned.clear(LiftoffRegister(old_index));
    1552           0 :       index = pinned.set(__ GetUnusedRegister(kGpReg, pinned)).gp();
    1553           0 :       if (index != old_index) __ Move(index, old_index, kWasmI32);
    1554             :     }
    1555           0 :     Register tmp = __ GetUnusedRegister(kGpReg, pinned).gp();
    1556           0 :     __ LoadConstant(LiftoffRegister(tmp), WasmValue(*offset));
    1557           0 :     __ emit_ptrsize_add(index, index, tmp);
    1558           0 :     LOAD_INSTANCE_FIELD(tmp, MemoryMask, kSystemPointerSize);
    1559           0 :     __ emit_ptrsize_and(index, index, tmp);
    1560           0 :     *offset = 0;
    1561           0 :     return index;
    1562             :   }
    1563             : 
    1564      169913 :   void LoadMem(FullDecoder* decoder, LoadType type,
    1565             :                const MemoryAccessImmediate<validate>& imm,
    1566             :                const Value& index_val, Value* result) {
    1567             :     ValueType value_type = type.value_type();
    1568       85391 :     if (!CheckSupportedType(decoder, kSupportedTypes, value_type, "load"))
    1569         858 :       return;
    1570       85391 :     LiftoffRegList pinned;
    1571      170781 :     Register index = pinned.set(__ PopToRegister()).gp();
    1572      170780 :     if (BoundsCheckMem(decoder, type.size(), imm.offset, index, pinned)) {
    1573             :       return;
    1574             :     }
    1575       84532 :     uint32_t offset = imm.offset;
    1576       84532 :     index = AddMemoryMasking(index, &offset, pinned);
    1577             :     DEBUG_CODE_COMMENT("Load from memory");
    1578       84532 :     Register addr = pinned.set(__ GetUnusedRegister(kGpReg, pinned)).gp();
    1579       84532 :     LOAD_INSTANCE_FIELD(addr, MemoryStart, kSystemPointerSize);
    1580             :     RegClass rc = reg_class_for(value_type);
    1581             :     LiftoffRegister value = pinned.set(__ GetUnusedRegister(rc, pinned));
    1582       84533 :     uint32_t protected_load_pc = 0;
    1583       84533 :     __ Load(value, addr, index, offset, type, pinned, &protected_load_pc, true);
    1584       84530 :     if (env_->use_trap_handler) {
    1585             :       AddOutOfLineTrap(decoder->position(),
    1586             :                        WasmCode::kThrowWasmTrapMemOutOfBounds,
    1587      168996 :                        protected_load_pc);
    1588             :     }
    1589             :     __ PushRegister(value_type, value);
    1590             : 
    1591       84533 :     if (FLAG_trace_wasm_memory) {
    1592             :       TraceMemoryOperation(false, type.mem_type().representation(), index,
    1593          48 :                            offset, decoder->position());
    1594             :     }
    1595             :   }
    1596             : 
    1597      226536 :   void StoreMem(FullDecoder* decoder, StoreType type,
    1598             :                 const MemoryAccessImmediate<validate>& imm,
    1599             :                 const Value& index_val, const Value& value_val) {
    1600             :     ValueType value_type = type.value_type();
    1601      113628 :     if (!CheckSupportedType(decoder, kSupportedTypes, value_type, "store"))
    1602         744 :       return;
    1603      113633 :     LiftoffRegList pinned;
    1604      227248 :     LiftoffRegister value = pinned.set(__ PopToRegister());
    1605      113615 :     Register index = pinned.set(__ PopToRegister(pinned)).gp();
    1606      227228 :     if (BoundsCheckMem(decoder, type.size(), imm.offset, index, pinned)) {
    1607             :       return;
    1608             :     }
    1609      112864 :     uint32_t offset = imm.offset;
    1610      112864 :     index = AddMemoryMasking(index, &offset, pinned);
    1611             :     DEBUG_CODE_COMMENT("Store to memory");
    1612      112874 :     Register addr = pinned.set(__ GetUnusedRegister(kGpReg, pinned)).gp();
    1613      112874 :     LOAD_INSTANCE_FIELD(addr, MemoryStart, kSystemPointerSize);
    1614      112894 :     uint32_t protected_store_pc = 0;
    1615             :     LiftoffRegList outer_pinned;
    1616      112894 :     if (FLAG_trace_wasm_memory) outer_pinned.set(index);
    1617             :     __ Store(addr, index, offset, value, type, outer_pinned,
    1618      112894 :              &protected_store_pc, true);
    1619      112891 :     if (env_->use_trap_handler) {
    1620             :       AddOutOfLineTrap(decoder->position(),
    1621             :                        WasmCode::kThrowWasmTrapMemOutOfBounds,
    1622      225784 :                        protected_store_pc);
    1623             :     }
    1624      112910 :     if (FLAG_trace_wasm_memory) {
    1625             :       TraceMemoryOperation(true, type.mem_rep(), index, offset,
    1626          32 :                            decoder->position());
    1627             :     }
    1628             :   }
    1629             : 
    1630         484 :   void CurrentMemoryPages(FullDecoder* decoder, Value* result) {
    1631         484 :     Register mem_size = __ GetUnusedRegister(kGpReg).gp();
    1632         484 :     LOAD_INSTANCE_FIELD(mem_size, MemorySize, kSystemPointerSize);
    1633             :     __ emit_ptrsize_shr(mem_size, mem_size, kWasmPageSizeLog2);
    1634             :     __ PushRegister(kWasmI32, LiftoffRegister(mem_size));
    1635         484 :   }
    1636             : 
    1637        1291 :   void MemoryGrow(FullDecoder* decoder, const Value& value, Value* result_val) {
    1638             :     // Pop the input, then spill all cache registers to make the runtime call.
    1639             :     LiftoffRegList pinned;
    1640        2582 :     LiftoffRegister input = pinned.set(__ PopToRegister());
    1641        1291 :     __ SpillAllRegisters();
    1642             : 
    1643             :     constexpr Register kGpReturnReg = kGpReturnRegisters[0];
    1644             :     static_assert(kLiftoffAssemblerGpCacheRegs & Register::bit<kGpReturnReg>(),
    1645             :                   "first return register is a cache register (needs more "
    1646             :                   "complex code here otherwise)");
    1647             :     LiftoffRegister result = pinned.set(LiftoffRegister(kGpReturnReg));
    1648             : 
    1649             :     WasmMemoryGrowDescriptor descriptor;
    1650             :     DCHECK_EQ(0, descriptor.GetStackParameterCount());
    1651             :     DCHECK_EQ(1, descriptor.GetRegisterParameterCount());
    1652             :     DCHECK_EQ(ValueTypes::MachineTypeFor(kWasmI32),
    1653             :               descriptor.GetParameterType(0));
    1654             : 
    1655             :     Register param_reg = descriptor.GetRegisterParameter(0);
    1656        1291 :     if (input.gp() != param_reg) __ Move(param_reg, input.gp(), kWasmI32);
    1657             : 
    1658             :     __ CallRuntimeStub(WasmCode::kWasmMemoryGrow);
    1659             :     safepoint_table_builder_.DefineSafepoint(&asm_, Safepoint::kSimple,
    1660        1290 :                                              Safepoint::kNoLazyDeopt);
    1661             : 
    1662             :     if (kReturnRegister0 != result.gp()) {
    1663             :       __ Move(result.gp(), kReturnRegister0, kWasmI32);
    1664             :     }
    1665             : 
    1666             :     __ PushRegister(kWasmI32, result);
    1667        1290 :   }
    1668             : 
    1669      243775 :   void CallDirect(FullDecoder* decoder,
    1670             :                   const CallFunctionImmediate<validate>& imm,
    1671             :                   const Value args[], Value returns[]) {
    1672      237002 :     if (imm.sig->return_count() > 1)
    1673             :       return unsupported(decoder, "multi-return");
    1674      236450 :     if (imm.sig->return_count() == 1 &&
    1675             :         !CheckSupportedType(decoder, kSupportedTypes, imm.sig->GetReturn(0),
    1676      114840 :                             "return"))
    1677             :       return;
    1678             : 
    1679             :     auto call_descriptor =
    1680      121610 :         compiler::GetWasmCallDescriptor(compilation_zone_, imm.sig);
    1681             :     call_descriptor =
    1682             :         GetLoweredCallDescriptor(compilation_zone_, call_descriptor);
    1683             : 
    1684      121611 :     if (imm.index < env_->module->num_imported_functions) {
    1685             :       // A direct call to an imported function.
    1686             :       LiftoffRegList pinned;
    1687      112897 :       Register tmp = pinned.set(__ GetUnusedRegister(kGpReg, pinned)).gp();
    1688      112897 :       Register target = pinned.set(__ GetUnusedRegister(kGpReg, pinned)).gp();
    1689             : 
    1690      112897 :       Register imported_targets = tmp;
    1691      112897 :       LOAD_INSTANCE_FIELD(imported_targets, ImportedFunctionTargets,
    1692             :                           kSystemPointerSize);
    1693             :       __ Load(LiftoffRegister(target), imported_targets, no_reg,
    1694      225794 :               imm.index * sizeof(Address), kPointerLoadType, pinned);
    1695             : 
    1696      112897 :       Register imported_function_refs = tmp;
    1697      112897 :       LOAD_TAGGED_PTR_INSTANCE_FIELD(imported_function_refs,
    1698             :                                      ImportedFunctionRefs);
    1699      112897 :       Register imported_function_ref = tmp;
    1700             :       __ LoadTaggedPointer(
    1701             :           imported_function_ref, imported_function_refs, no_reg,
    1702      225794 :           ObjectAccess::ElementOffsetInTaggedFixedArray(imm.index), pinned);
    1703             : 
    1704             :       Register* explicit_instance = &imported_function_ref;
    1705      112897 :       __ PrepareCall(imm.sig, call_descriptor, &target, explicit_instance);
    1706             :       source_position_table_builder_.AddPosition(
    1707      234510 :           __ pc_offset(), SourcePosition(decoder->position()), false);
    1708             : 
    1709      112897 :       __ CallIndirect(imm.sig, call_descriptor, target);
    1710             : 
    1711             :       safepoint_table_builder_.DefineSafepoint(&asm_, Safepoint::kSimple,
    1712      112897 :                                                Safepoint::kNoLazyDeopt);
    1713             : 
    1714      112897 :       __ FinishCall(imm.sig, call_descriptor);
    1715             :     } else {
    1716             :       // A direct call within this module just gets the current instance.
    1717        8714 :       __ PrepareCall(imm.sig, call_descriptor);
    1718             : 
    1719             :       source_position_table_builder_.AddPosition(
    1720        8716 :           __ pc_offset(), SourcePosition(decoder->position()), false);
    1721             : 
    1722             :       // Just encode the function index. This will be patched at instantiation.
    1723        8716 :       Address addr = static_cast<Address>(imm.index);
    1724             :       __ CallNativeWasmCode(addr);
    1725             : 
    1726             :       safepoint_table_builder_.DefineSafepoint(&asm_, Safepoint::kSimple,
    1727        8714 :                                                Safepoint::kNoLazyDeopt);
    1728             : 
    1729        8716 :       __ FinishCall(imm.sig, call_descriptor);
    1730             :     }
    1731             :   }
    1732             : 
    1733       10996 :   void CallIndirect(FullDecoder* decoder, const Value& index_val,
    1734             :                     const CallIndirectImmediate<validate>& imm,
    1735             :                     const Value args[], Value returns[]) {
    1736        4317 :     if (imm.sig->return_count() > 1) {
    1737           0 :       return unsupported(decoder, "multi-return");
    1738             :     }
    1739        4318 :     if (imm.sig->return_count() == 1 &&
    1740             :         !CheckSupportedType(decoder, kSupportedTypes, imm.sig->GetReturn(0),
    1741        1569 :                             "return")) {
    1742             :       return;
    1743             :     }
    1744             : 
    1745             :     // Pop the index.
    1746        5500 :     Register index = __ PopToRegister().gp();
    1747             :     // If that register is still being used after popping, we move it to another
    1748             :     // register, because we want to modify that register.
    1749        2751 :     if (__ cache_state()->is_used(LiftoffRegister(index))) {
    1750             :       Register new_index =
    1751             :           __ GetUnusedRegister(kGpReg, LiftoffRegList::ForRegs(index)).gp();
    1752         592 :       __ Move(new_index, index, kWasmI32);
    1753             :       index = new_index;
    1754             :     }
    1755             : 
    1756             :     LiftoffRegList pinned = LiftoffRegList::ForRegs(index);
    1757             :     // Get three temporary registers.
    1758             :     Register table = pinned.set(__ GetUnusedRegister(kGpReg, pinned)).gp();
    1759        2750 :     Register tmp_const = pinned.set(__ GetUnusedRegister(kGpReg, pinned)).gp();
    1760             :     Register scratch = pinned.set(__ GetUnusedRegister(kGpReg, pinned)).gp();
    1761             : 
    1762             :     // Bounds check against the table size.
    1763             :     Label* invalid_func_label = AddOutOfLineTrap(
    1764        2751 :         decoder->position(), WasmCode::kThrowWasmTrapFuncInvalid);
    1765             : 
    1766        5500 :     uint32_t canonical_sig_num = env_->module->signature_ids[imm.sig_index];
    1767             :     DCHECK_GE(canonical_sig_num, 0);
    1768             :     DCHECK_GE(kMaxInt, canonical_sig_num);
    1769             : 
    1770             :     // Compare against table size stored in
    1771             :     // {instance->indirect_function_table_size}.
    1772        2750 :     LOAD_INSTANCE_FIELD(tmp_const, IndirectFunctionTableSize, kUInt32Size);
    1773             :     __ emit_cond_jump(kUnsignedGreaterEqual, invalid_func_label, kWasmI32,
    1774        2751 :                       index, tmp_const);
    1775             : 
    1776             :     // Mask the index to prevent SSCA.
    1777        2751 :     if (FLAG_untrusted_code_mitigations) {
    1778             :       DEBUG_CODE_COMMENT("Mask indirect call index");
    1779             :       // mask = ((index - size) & ~index) >> 31
    1780             :       // Reuse allocated registers; note: size is still stored in {tmp_const}.
    1781             :       Register diff = table;
    1782           0 :       Register neg_index = tmp_const;
    1783             :       Register mask = scratch;
    1784             :       // 1) diff = index - size
    1785           0 :       __ emit_i32_sub(diff, index, tmp_const);
    1786             :       // 2) neg_index = ~index
    1787           0 :       __ LoadConstant(LiftoffRegister(neg_index), WasmValue(int32_t{-1}));
    1788             :       __ emit_i32_xor(neg_index, neg_index, index);
    1789             :       // 3) mask = diff & neg_index
    1790             :       __ emit_i32_and(mask, diff, neg_index);
    1791             :       // 4) mask = mask >> 31
    1792           0 :       __ LoadConstant(LiftoffRegister(tmp_const), WasmValue(int32_t{31}));
    1793             :       __ emit_i32_sar(mask, mask, tmp_const, pinned);
    1794             : 
    1795             :       // Apply mask.
    1796             :       __ emit_i32_and(index, index, mask);
    1797             :     }
    1798             : 
    1799             :     DEBUG_CODE_COMMENT("Check indirect call signature");
    1800             :     // Load the signature from {instance->ift_sig_ids[key]}
    1801        2751 :     LOAD_INSTANCE_FIELD(table, IndirectFunctionTableSigIds, kSystemPointerSize);
    1802             :     __ LoadConstant(LiftoffRegister(tmp_const),
    1803        5495 :                     WasmValue(static_cast<uint32_t>(sizeof(uint32_t))));
    1804             :     // TODO(wasm): use a emit_i32_shli() instead of a multiply.
    1805             :     // (currently cannot use shl on ia32/x64 because it clobbers %rcx).
    1806             :     __ emit_i32_mul(index, index, tmp_const);
    1807             :     __ Load(LiftoffRegister(scratch), table, index, 0, LoadType::kI32Load,
    1808        2751 :             pinned);
    1809             : 
    1810             :     // Compare against expected signature.
    1811        5498 :     __ LoadConstant(LiftoffRegister(tmp_const), WasmValue(canonical_sig_num));
    1812             : 
    1813             :     Label* sig_mismatch_label = AddOutOfLineTrap(
    1814        2747 :         decoder->position(), WasmCode::kThrowWasmTrapFuncSigMismatch);
    1815             :     __ emit_cond_jump(kUnequal, sig_mismatch_label,
    1816        2751 :                       LiftoffAssembler::kWasmIntPtr, scratch, tmp_const);
    1817             : 
    1818             :     DEBUG_CODE_COMMENT("Execute indirect call");
    1819             :     if (kSystemPointerSize == 8) {
    1820             :       // {index} has already been multiplied by 4. Multiply by another 2.
    1821        5500 :       __ LoadConstant(LiftoffRegister(tmp_const), WasmValue(2));
    1822             :       __ emit_i32_mul(index, index, tmp_const);
    1823             :     }
    1824             : 
    1825             :     // Load the instance from {instance->ift_instances[key]}
    1826        2751 :     LOAD_TAGGED_PTR_INSTANCE_FIELD(table, IndirectFunctionTableRefs);
    1827             :     // {index} has already been multiplied by kSystemPointerSizeLog2.
    1828             :     STATIC_ASSERT(kTaggedSize == kSystemPointerSize);
    1829             :     __ LoadTaggedPointer(tmp_const, table, index,
    1830             :                          ObjectAccess::ElementOffsetInTaggedFixedArray(0),
    1831        2747 :                          pinned);
    1832             :     Register* explicit_instance = &tmp_const;
    1833             : 
    1834             :     // Load the target from {instance->ift_targets[key]}
    1835        2751 :     LOAD_INSTANCE_FIELD(table, IndirectFunctionTableTargets,
    1836             :                         kSystemPointerSize);
    1837             :     __ Load(LiftoffRegister(scratch), table, index, 0, kPointerLoadType,
    1838        2750 :             pinned);
    1839             : 
    1840             :     source_position_table_builder_.AddPosition(
    1841        5500 :         __ pc_offset(), SourcePosition(decoder->position()), false);
    1842             : 
    1843             :     auto call_descriptor =
    1844        2751 :         compiler::GetWasmCallDescriptor(compilation_zone_, imm.sig);
    1845             :     call_descriptor =
    1846             :         GetLoweredCallDescriptor(compilation_zone_, call_descriptor);
    1847             : 
    1848        2751 :     Register target = scratch;
    1849        2751 :     __ PrepareCall(imm.sig, call_descriptor, &target, explicit_instance);
    1850        2750 :     __ CallIndirect(imm.sig, call_descriptor, target);
    1851             : 
    1852             :     safepoint_table_builder_.DefineSafepoint(&asm_, Safepoint::kSimple,
    1853        2751 :                                              Safepoint::kNoLazyDeopt);
    1854             : 
    1855        2751 :     __ FinishCall(imm.sig, call_descriptor);
    1856             :   }
    1857           0 :   void ReturnCall(FullDecoder* decoder,
    1858             :                   const CallFunctionImmediate<validate>& imm,
    1859             :                   const Value args[]) {
    1860             :     unsupported(decoder, "return_call");
    1861           0 :   }
    1862             : 
    1863           0 :   void ReturnCallIndirect(FullDecoder* decoder, const Value& index_val,
    1864             :                           const CallIndirectImmediate<validate>& imm,
    1865             :                           const Value args[]) {
    1866             :     unsupported(decoder, "return_call_indirect");
    1867           0 :   }
    1868           0 :   void SimdOp(FullDecoder* decoder, WasmOpcode opcode, Vector<Value> args,
    1869             :               Value* result) {
    1870             :     unsupported(decoder, "simd");
    1871           0 :   }
    1872           0 :   void SimdLaneOp(FullDecoder* decoder, WasmOpcode opcode,
    1873             :                   const SimdLaneImmediate<validate>& imm,
    1874             :                   const Vector<Value> inputs, Value* result) {
    1875             :     unsupported(decoder, "simd");
    1876           0 :   }
    1877           0 :   void SimdShiftOp(FullDecoder* decoder, WasmOpcode opcode,
    1878             :                    const SimdShiftImmediate<validate>& imm, const Value& input,
    1879             :                    Value* result) {
    1880             :     unsupported(decoder, "simd");
    1881           0 :   }
    1882           0 :   void Simd8x16ShuffleOp(FullDecoder* decoder,
    1883             :                          const Simd8x16ShuffleImmediate<validate>& imm,
    1884             :                          const Value& input0, const Value& input1,
    1885             :                          Value* result) {
    1886             :     unsupported(decoder, "simd");
    1887           0 :   }
    1888         128 :   void Throw(FullDecoder* decoder, const ExceptionIndexImmediate<validate>&,
    1889             :              const Vector<Value>& args) {
    1890             :     unsupported(decoder, "throw");
    1891         128 :   }
    1892           0 :   void Rethrow(FullDecoder* decoder, const Value& exception) {
    1893             :     unsupported(decoder, "rethrow");
    1894           0 :   }
    1895           0 :   void BrOnException(FullDecoder* decoder, const Value& exception,
    1896             :                      const ExceptionIndexImmediate<validate>& imm,
    1897             :                      uint32_t depth, Vector<Value> values) {
    1898             :     unsupported(decoder, "br_on_exn");
    1899           0 :   }
    1900       14695 :   void AtomicOp(FullDecoder* decoder, WasmOpcode opcode, Vector<Value> args,
    1901             :                 const MemoryAccessImmediate<validate>& imm, Value* result) {
    1902             :     unsupported(decoder, "atomicop");
    1903       14701 :   }
    1904          32 :   void MemoryInit(FullDecoder* decoder,
    1905             :                   const MemoryInitImmediate<validate>& imm, const Value& dst,
    1906             :                   const Value& src, const Value& size) {
    1907             :     unsupported(decoder, "memory.init");
    1908          32 :   }
    1909          16 :   void DataDrop(FullDecoder* decoder, const DataDropImmediate<validate>& imm) {
    1910             :     unsupported(decoder, "data.drop");
    1911          16 :   }
    1912          24 :   void MemoryCopy(FullDecoder* decoder,
    1913             :                   const MemoryCopyImmediate<validate>& imm, const Value& dst,
    1914             :                   const Value& src, const Value& size) {
    1915             :     unsupported(decoder, "memory.copy");
    1916          24 :   }
    1917          24 :   void MemoryFill(FullDecoder* decoder,
    1918             :                   const MemoryIndexImmediate<validate>& imm, const Value& dst,
    1919             :                   const Value& value, const Value& size) {
    1920             :     unsupported(decoder, "memory.fill");
    1921          24 :   }
    1922          16 :   void TableInit(FullDecoder* decoder, const TableInitImmediate<validate>& imm,
    1923             :                  Vector<Value> args) {
    1924             :     unsupported(decoder, "table.init");
    1925          16 :   }
    1926          16 :   void ElemDrop(FullDecoder* decoder, const ElemDropImmediate<validate>& imm) {
    1927             :     unsupported(decoder, "elem.drop");
    1928          16 :   }
    1929          40 :   void TableCopy(FullDecoder* decoder, const TableCopyImmediate<validate>& imm,
    1930             :                  Vector<Value> args) {
    1931             :     unsupported(decoder, "table.copy");
    1932          40 :   }
    1933             : 
    1934             :  private:
    1935             :   LiftoffAssembler asm_;
    1936             :   compiler::CallDescriptor* const descriptor_;
    1937             :   CompilationEnv* const env_;
    1938             :   bool ok_ = true;
    1939             :   std::vector<OutOfLineCode> out_of_line_code_;
    1940             :   SourcePositionTableBuilder source_position_table_builder_;
    1941             :   std::vector<trap_handler::ProtectedInstructionData> protected_instructions_;
    1942             :   // Zone used to store information during compilation. The result will be
    1943             :   // stored independently, such that this zone can die together with the
    1944             :   // LiftoffCompiler after compilation.
    1945             :   Zone* compilation_zone_;
    1946             :   SafepointTableBuilder safepoint_table_builder_;
    1947             :   // The pc offset of the instructions to reserve the stack frame. Needed to
    1948             :   // patch the actually needed stack size in the end.
    1949             :   uint32_t pc_offset_stack_frame_construction_ = 0;
    1950             : 
    1951             :   void TraceCacheState(FullDecoder* decoder) const {
    1952             : #ifdef DEBUG
    1953             :     if (!FLAG_trace_liftoff || !FLAG_trace_wasm_decoder) return;
    1954             :     StdoutStream os;
    1955             :     for (int control_depth = decoder->control_depth() - 1; control_depth >= -1;
    1956             :          --control_depth) {
    1957             :       auto* cache_state =
    1958             :           control_depth == -1 ? __ cache_state()
    1959             :                               : &decoder->control_at(control_depth)
    1960             :                                      ->label_state;
    1961             :       os << PrintCollection(cache_state->stack_state);
    1962             :       if (control_depth != -1) PrintF("; ");
    1963             :     }
    1964             :     os << "\n";
    1965             : #endif
    1966             :   }
    1967             : 
    1968             :   DISALLOW_IMPLICIT_CONSTRUCTORS(LiftoffCompiler);
    1969             : };
    1970             : 
    1971             : }  // namespace
    1972             : 
    1973      594193 : WasmCompilationResult LiftoffCompilationUnit::ExecuteCompilation(
    1974             :     CompilationEnv* env, const FunctionBody& func_body, Counters* counters,
    1975             :     WasmFeatures* detected) {
    1976     1188394 :   TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.wasm"),
    1977             :                "ExecuteLiftoffCompilation");
    1978             :   base::ElapsedTimer compile_timer;
    1979             :   if (FLAG_trace_wasm_decode_time) {
    1980             :     compile_timer.Start();
    1981             :   }
    1982             : 
    1983     1188305 :   Zone zone(wasm_unit_->wasm_engine_->allocator(), "LiftoffCompilationZone");
    1984      594163 :   const WasmModule* module = env ? env->module : nullptr;
    1985      594163 :   auto call_descriptor = compiler::GetWasmCallDescriptor(&zone, func_body.sig);
    1986             :   base::Optional<TimedHistogramScope> liftoff_compile_time_scope(
    1987             :       base::in_place, counters->liftoff_compile_time());
    1988             :   std::unique_ptr<wasm::WasmInstructionBuffer> instruction_buffer =
    1989      594181 :       wasm::WasmInstructionBuffer::New();
    1990             :   WasmFullDecoder<Decoder::kValidate, LiftoffCompiler> decoder(
    1991             :       &zone, module, env->enabled_features, detected, func_body,
    1992     1782372 :       call_descriptor, env, &zone, instruction_buffer->CreateView());
    1993      594178 :   decoder.Decode();
    1994             :   liftoff_compile_time_scope.reset();
    1995      570041 :   LiftoffCompiler* compiler = &decoder.interface();
    1996      593942 :   if (decoder.failed()) {
    1997             :     compiler->OnFirstError(&decoder);
    1998       47850 :     return WasmCompilationResult{decoder.error()};
    1999             :   }
    2000      570041 :   if (!compiler->ok()) {
    2001             :     // Liftoff compilation failed.
    2002           0 :     counters->liftoff_unsupported_functions()->Increment();
    2003           0 :     return WasmCompilationResult{WasmError{0, "Liftoff bailout"}};
    2004             :   }
    2005             : 
    2006      570041 :   counters->liftoff_compiled_functions()->Increment();
    2007             : 
    2008             :   if (FLAG_trace_wasm_decode_time) {
    2009             :     double compile_ms = compile_timer.Elapsed().InMillisecondsF();
    2010             :     PrintF(
    2011             :         "wasm-compilation liftoff phase 1 ok: %u bytes, %0.3f ms decode and "
    2012             :         "compile\n",
    2013             :         static_cast<unsigned>(func_body.end - func_body.start), compile_ms);
    2014             :   }
    2015             : 
    2016     1140042 :   WasmCompilationResult result;
    2017             :   compiler->GetCode(&result.code_desc);
    2018     1140157 :   result.instr_buffer = instruction_buffer->ReleaseBuffer();
    2019     1140234 :   result.source_positions = compiler->GetSourcePositionTable();
    2020     1140301 :   result.protected_instructions = compiler->GetProtectedInstructions();
    2021      570135 :   result.frame_slot_count = compiler->GetTotalFrameSlotCount();
    2022      570135 :   result.tagged_parameter_slots = call_descriptor->GetTaggedParameterSlots();
    2023             : 
    2024             :   DCHECK(result.succeeded());
    2025     1164410 :   return result;
    2026             : }
    2027             : 
    2028             : #undef __
    2029             : #undef TRACE
    2030             : #undef WASM_INSTANCE_OBJECT_FIELD_OFFSET
    2031             : #undef WASM_INSTANCE_OBJECT_FIELD_SIZE
    2032             : #undef LOAD_INSTANCE_FIELD
    2033             : #undef LOAD_TAGGED_PTR_INSTANCE_FIELD
    2034             : #undef DEBUG_CODE_COMMENT
    2035             : 
    2036             : }  // namespace wasm
    2037             : }  // namespace internal
    2038      178779 : }  // namespace v8

Generated by: LCOV version 1.10