Bug 1288944 - Baldr: factor out MWasmCall::Callee and TableDesc (r=bbouvier)
authorLuke Wagner <luke@mozilla.com>
Fri, 05 Aug 2016 15:39:56 -0500
changeset 397528 71d0aa664ad1eec32432e72e80fb7089bb590b62
parent 397527 5f028ac66f96fe761149751a32304e72f28880cf
child 397529 fe89cf4e4a9e4a9444f1c3391ef017e0ed4c28cc
push id25332
push usermaglione.k@gmail.com
push dateSat, 06 Aug 2016 21:21:51 +0000
reviewersbbouvier
bugs1288944
milestone51.0a1
Bug 1288944 - Baldr: factor out MWasmCall::Callee and TableDesc (r=bbouvier) MozReview-Commit-ID: 39hlX5PBW9M
js/src/asmjs/WasmCode.h
js/src/asmjs/WasmIonCompile.cpp
js/src/asmjs/WasmTypes.h
js/src/jit/MIR.cpp
js/src/jit/MIR.h
js/src/jit/arm/CodeGenerator-arm.cpp
js/src/jit/shared/CodeGenerator-shared.cpp
js/src/jit/x86/CodeGenerator-x86.cpp
--- a/js/src/asmjs/WasmCode.h
+++ b/js/src/asmjs/WasmCode.h
@@ -218,40 +218,16 @@ class FuncImport
         return pod.jitExitCodeOffset_;
     }
 
     WASM_DECLARE_SERIALIZABLE(FuncImport)
 };
 
 typedef Vector<FuncImport, 0, SystemAllocPolicy> FuncImportVector;
 
-// TableDesc contains the metadata describing a table as well as the
-// module-specific offset of the table's base pointer in global memory.
-// The element kind of this table. Currently, wasm only has "any function" and
-// asm.js only "typed function".
-
-enum class TableKind
-{
-    AnyFunction,
-    TypedFunction
-};
-
-struct TableDesc
-{
-    TableKind kind;
-    bool external;
-    uint32_t globalDataOffset;
-    uint32_t initial;
-    uint32_t maximum;
-
-    TableDesc() { PodZero(this); }
-};
-
-WASM_DECLARE_POD_VECTOR(TableDesc, TableDescVector)
-
 // A CodeRange describes a single contiguous range of code within a wasm
 // module's code segment. A CodeRange describes what the code does and, for
 // function bodies, the name and source coordinates of the function.
 
 class CodeRange
 {
   public:
     enum Kind { Function, Entry, ImportJitExit, ImportInterpExit, Inline, CallThunk };
--- a/js/src/asmjs/WasmIonCompile.cpp
+++ b/js/src/asmjs/WasmIonCompile.cpp
@@ -975,17 +975,17 @@ class FunctionCompiler
     {
         if (inDeadCode()) {
             *def = nullptr;
             return true;
         }
 
         CallSiteDesc desc(call.lineOrBytecode_, CallSiteDesc::Relative);
         MIRType ret = ToMIRType(sig.ret());
-        auto callee = MWasmCall::Callee::internal(funcIndex);
+        auto callee = CalleeDesc::internal(funcIndex);
         auto* ins = MWasmCall::New(alloc(), desc, callee, call.regArgs_, ret, call.spIncrement_);
         if (!ins)
             return false;
 
         curBlock_->add(ins);
         *def = ins;
         return true;
     }
@@ -995,35 +995,35 @@ class FunctionCompiler
     {
         if (inDeadCode()) {
             *def = nullptr;
             return true;
         }
 
         const SigWithId& sig = mg_.sigs[sigIndex];
 
-        MWasmCall::Callee callee;
+        CalleeDesc callee;
         if (mg_.isAsmJS()) {
             const TableDesc& table = mg_.tables[mg_.asmJSSigToTableIndex[sigIndex]];
             MOZ_ASSERT(sig.id.kind() == SigIdDesc::Kind::None);
             MOZ_ASSERT(IsPowerOfTwo(table.initial));
 
             MConstant* mask = MConstant::New(alloc(), Int32Value(table.initial - 1));
             curBlock_->add(mask);
             MBitAnd* maskedIndex = MBitAnd::NewAsmJS(alloc(), index, mask, MIRType::Int32);
             curBlock_->add(maskedIndex);
 
             index = maskedIndex;
-            callee = MWasmCall::Callee::asmJSTable(table.globalDataOffset);
+            callee = CalleeDesc::asmJSTable(table);
         } else {
             const TableDesc& table = mg_.tables[0];
             MOZ_ASSERT(sig.id.kind() != SigIdDesc::Kind::None);
             MOZ_ASSERT(mg_.tables.length() == 1);
 
-            callee = MWasmCall::Callee::wasmTable(table.globalDataOffset, table.initial, sig.id);
+            callee = CalleeDesc::wasmTable(table, sig.id);
         }
 
         CallSiteDesc desc(call.lineOrBytecode_, CallSiteDesc::Register);
         MIRType ret = ToMIRType(sig.ret());
         auto* ins = MWasmCall::New(alloc(), desc, callee, call.regArgs_, ret, call.spIncrement_, index);
         if (!ins)
             return false;
 
@@ -1040,17 +1040,17 @@ class FunctionCompiler
             return true;
         }
 
         MOZ_ASSERT(call.tlsStackOffset_ != UINT32_MAX);
 
 
         CallSiteDesc desc(call.lineOrBytecode_, CallSiteDesc::Register);
         MIRType ret = ToMIRType(exprRet);
-        auto callee = MWasmCall::Callee::import(globalDataOffset, call.tlsStackOffset_);
+        auto callee = CalleeDesc::import(globalDataOffset, call.tlsStackOffset_);
         auto* ins = MWasmCall::New(alloc(), desc, callee, call.regArgs_, ret, call.spIncrement_);
         if (!ins)
             return false;
 
         curBlock_->add(ins);
         *def = ins;
         return true;
     }
@@ -1060,17 +1060,17 @@ class FunctionCompiler
     {
         if (inDeadCode()) {
             *def = nullptr;
             return true;
         }
 
         CallSiteDesc desc(call.lineOrBytecode_, CallSiteDesc::Register);
         MIRType ret = ToMIRType(valRet);
-        auto callee = MWasmCall::Callee(builtin);
+        auto callee = CalleeDesc::builtin(builtin);
         auto* ins = MWasmCall::New(alloc(), desc, callee, call.regArgs_, ret, call.spIncrement_);
         if (!ins)
             return false;
 
         curBlock_->add(ins);
         *def = ins;
         return true;
     }
--- a/js/src/asmjs/WasmTypes.h
+++ b/js/src/asmjs/WasmTypes.h
@@ -1035,16 +1035,133 @@ struct Assumptions
 // A Module can either be asm.js or wasm.
 
 enum ModuleKind
 {
     Wasm,
     AsmJS
 };
 
+// TableDesc describes a table as well as the offset of the table's base pointer
+// in global memory. Currently, wasm only has "any function" and asm.js only
+// "typed function".
+
+enum class TableKind
+{
+    AnyFunction,
+    TypedFunction
+};
+
+struct TableDesc
+{
+    TableKind kind;
+    bool external;
+    uint32_t globalDataOffset;
+    uint32_t initial;
+    uint32_t maximum;
+
+    TableDesc() { PodZero(this); }
+};
+
+WASM_DECLARE_POD_VECTOR(TableDesc, TableDescVector)
+
+// CalleeDesc describes how to compile one of the variety of asm.js/wasm calls.
+// This is hoisted into WasmTypes.h for sharing between Ion and Baseline.
+
+class CalleeDesc
+{
+  public:
+    enum Which { Internal, Import, WasmTable, AsmJSTable, Builtin };
+
+  private:
+    Which which_;
+    union U {
+        U() {}
+        uint32_t internalFuncIndex_;
+        struct {
+            uint32_t globalDataOffset_;
+            uint32_t tlsStackOffset_;
+        } import;
+        struct {
+            TableDesc desc_;
+            SigIdDesc sigId_;
+        } table;
+        SymbolicAddress builtin_;
+    } u;
+
+  public:
+    CalleeDesc() {}
+    static CalleeDesc internal(uint32_t callee) {
+        CalleeDesc c;
+        c.which_ = Internal;
+        c.u.internalFuncIndex_ = callee;
+        return c;
+    }
+    static CalleeDesc import(uint32_t globalDataOffset, uint32_t tlsStackOffset) {
+        CalleeDesc c;
+        c.which_ = Import;
+        c.u.import.globalDataOffset_ = globalDataOffset;
+        c.u.import.tlsStackOffset_ = tlsStackOffset;
+        return c;
+    }
+    static CalleeDesc wasmTable(const TableDesc& desc, SigIdDesc sigId) {
+        CalleeDesc c;
+        c.which_ = WasmTable;
+        c.u.table.desc_ = desc;
+        c.u.table.sigId_ = sigId;
+        return c;
+    }
+    static CalleeDesc asmJSTable(const TableDesc& desc) {
+        CalleeDesc c;
+        c.which_ = AsmJSTable;
+        c.u.table.desc_ = desc;
+        return c;
+    }
+    static CalleeDesc builtin(SymbolicAddress callee) {
+        CalleeDesc c;
+        c.which_ = Builtin;
+        c.u.builtin_ = callee;
+        return c;
+    }
+    Which which() const {
+        return which_;
+    }
+    uint32_t internalFuncIndex() const {
+        MOZ_ASSERT(which_ == Internal);
+        return u.internalFuncIndex_;
+    }
+    uint32_t importGlobalDataOffset() const {
+        MOZ_ASSERT(which_ == Import);
+        return u.import.globalDataOffset_;
+    }
+    uint32_t importTlsStackOffset() const {
+        MOZ_ASSERT(which_ == Import);
+        return u.import.tlsStackOffset_;
+    }
+    bool isTable() const {
+        return which_ == WasmTable || which_ == AsmJSTable;
+    }
+    uint32_t tableGlobalDataOffset() const {
+        MOZ_ASSERT(isTable());
+        return u.table.desc_.globalDataOffset;
+    }
+    uint32_t wasmTableLength() const {
+        MOZ_ASSERT(which_ == WasmTable);
+        return u.table.desc_.initial;
+    }
+    SigIdDesc wasmTableSigId() const {
+        MOZ_ASSERT(which_ == WasmTable);
+        return u.table.sigId_;
+    }
+    SymbolicAddress builtin() const {
+        MOZ_ASSERT(which_ == Builtin);
+        return u.builtin_;
+    }
+};
+
 // ExportArg holds the unboxed operands to the wasm entry trampoline which can
 // be called through an ExportFuncPtr.
 
 struct ExportArg
 {
     uint64_t lo;
     uint64_t hi;
 };
--- a/js/src/jit/MIR.cpp
+++ b/js/src/jit/MIR.cpp
@@ -5353,17 +5353,17 @@ MAsmJSUnsignedToFloat32::foldsTo(TempAll
         if (IsFloat32Representable(dval))
             return MConstant::NewAsmJS(alloc, JS::Float32Value(float(dval)), MIRType::Float32);
     }
 
     return this;
 }
 
 MWasmCall*
-MWasmCall::New(TempAllocator& alloc, const wasm::CallSiteDesc& desc, Callee callee,
+MWasmCall::New(TempAllocator& alloc, const wasm::CallSiteDesc& desc, const wasm::CalleeDesc& callee,
                const Args& args, MIRType resultType, size_t spIncrement,
                MDefinition* tableIndex)
 {
     MWasmCall* call = new(alloc) MWasmCall(desc, callee, spIncrement);
     call->setResultType(resultType);
 
     if (!call->argRegs_.init(alloc, args.length()))
         return nullptr;
--- a/js/src/jit/MIR.h
+++ b/js/src/jit/MIR.h
@@ -13513,141 +13513,52 @@ class MAsmJSPassStackArg
         spOffset_ += inc;
     }
 };
 
 class MWasmCall final
   : public MVariadicInstruction,
     public NoTypePolicy::Data
 {
-  public:
-    class Callee {
-      public:
-        enum Which { Internal, Import, WasmTable, AsmJSTable, Builtin };
-      private:
-        Which which_;
-        union U {
-            U() {}
-            uint32_t internalFuncIndex_;
-            struct {
-                uint32_t globalDataOffset_;
-                uint32_t tlsStackOffset_;
-            } import;
-            struct {
-                uint32_t globalDataOffset_;
-                uint32_t length_;
-                wasm::SigIdDesc sigId_;
-            } table;
-            wasm::SymbolicAddress builtin_;
-        } u;
-      public:
-        Callee() {}
-        static Callee internal(uint32_t callee) {
-            Callee c;
-            c.which_ = Internal;
-            c.u.internalFuncIndex_ = callee;
-            return c;
-        }
-        static Callee import(uint32_t globalDataOffset, uint32_t tlsStackOffset) {
-            Callee c;
-            c.which_ = Import;
-            c.u.import.globalDataOffset_ = globalDataOffset;
-            c.u.import.tlsStackOffset_ = tlsStackOffset;
-            return c;
-        }
-        static Callee wasmTable(uint32_t globalDataOffset, uint32_t length, wasm::SigIdDesc sigId) {
-            Callee c;
-            c.which_ = WasmTable;
-            c.u.table.globalDataOffset_ = globalDataOffset;
-            c.u.table.length_ = length;
-            c.u.table.sigId_ = sigId;
-            return c;
-        }
-        static Callee asmJSTable(uint32_t globalDataOffset) {
-            Callee c;
-            c.which_ = AsmJSTable;
-            c.u.table.globalDataOffset_ = globalDataOffset;
-            return c;
-        }
-        explicit Callee(wasm::SymbolicAddress callee) : which_(Builtin) {
-            u.builtin_ = callee;
-        }
-        Which which() const {
-            return which_;
-        }
-        uint32_t internalFuncIndex() const {
-            MOZ_ASSERT(which_ == Internal);
-            return u.internalFuncIndex_;
-        }
-        uint32_t importGlobalDataOffset() const {
-            MOZ_ASSERT(which_ == Import);
-            return u.import.globalDataOffset_;
-        }
-        uint32_t importTlsStackOffset() const {
-            MOZ_ASSERT(which_ == Import);
-            return u.import.tlsStackOffset_;
-        }
-        bool isTable() const {
-            return which_ == WasmTable || which_ == AsmJSTable;
-        }
-        uint32_t tableGlobalDataOffset() const {
-            MOZ_ASSERT(isTable());
-            return u.table.globalDataOffset_;
-        }
-        uint32_t wasmTableLength() const {
-            MOZ_ASSERT(which_ == WasmTable);
-            return u.table.length_;
-        }
-        wasm::SigIdDesc wasmTableSigId() const {
-            MOZ_ASSERT(which_ == WasmTable);
-            return u.table.sigId_;
-        }
-        wasm::SymbolicAddress builtin() const {
-            MOZ_ASSERT(which_ == Builtin);
-            return u.builtin_;
-        }
-    };
-
-  private:
     wasm::CallSiteDesc desc_;
-    Callee callee_;
+    wasm::CalleeDesc callee_;
     FixedList<AnyRegister> argRegs_;
     size_t spIncrement_;
 
-    MWasmCall(const wasm::CallSiteDesc& desc, Callee callee, size_t spIncrement)
+    MWasmCall(const wasm::CallSiteDesc& desc, const wasm::CalleeDesc& callee, size_t spIncrement)
       : desc_(desc),
         callee_(callee),
         spIncrement_(spIncrement)
     { }
 
   public:
     INSTRUCTION_HEADER(WasmCall)
 
     struct Arg {
         AnyRegister reg;
         MDefinition* def;
         Arg(AnyRegister reg, MDefinition* def) : reg(reg), def(def) {}
     };
     typedef Vector<Arg, 8, SystemAllocPolicy> Args;
 
-    static MWasmCall* New(TempAllocator& alloc, const wasm::CallSiteDesc& desc, Callee callee,
-                          const Args& args, MIRType resultType, size_t spIncrement,
-                          MDefinition* tableIndex = nullptr);
+    static MWasmCall* New(TempAllocator& alloc, const wasm::CallSiteDesc& desc,
+                          const wasm::CalleeDesc& callee, const Args& args, MIRType resultType,
+                          size_t spIncrement, MDefinition* tableIndex = nullptr);
 
     size_t numArgs() const {
         return argRegs_.length();
     }
     AnyRegister registerForArg(size_t index) const {
         MOZ_ASSERT(index < numArgs());
         return argRegs_[index];
     }
     const wasm::CallSiteDesc& desc() const {
         return desc_;
     }
-    Callee callee() const {
+    const wasm::CalleeDesc &callee() const {
         return callee_;
     }
     size_t spIncrement() const {
         return spIncrement_;
     }
 
     bool possiblyCalls() const override {
         return true;
--- a/js/src/jit/arm/CodeGenerator-arm.cpp
+++ b/js/src/jit/arm/CodeGenerator-arm.cpp
@@ -2183,17 +2183,17 @@ CodeGeneratorARM::visitAsmReinterpret(LA
     }
 }
 
 void
 CodeGeneratorARM::emitWasmCall(LWasmCallBase* ins)
 {
     MWasmCall* mir = ins->mir();
 
-    if (UseHardFpABI() || mir->callee().which() != MWasmCall::Callee::Builtin) {
+    if (UseHardFpABI() || mir->callee().which() != wasm::CalleeDesc::Builtin) {
         emitWasmCallBase(ins);
         return;
     }
 
     // The soft ABI passes floating point arguments in GPRs. Since basically
     // nothing is set up to handle this, the values are placed in the
     // corresponding VFP registers, then transferred to GPRs immediately
     // before the call. The mapping is sN <-> rN, where double registers
--- a/js/src/jit/shared/CodeGenerator-shared.cpp
+++ b/js/src/jit/shared/CodeGenerator-shared.cpp
@@ -1503,23 +1503,23 @@ CodeGeneratorShared::emitWasmCallBase(LW
 
 #ifdef DEBUG
     Label ok;
     masm.branchTestStackPtr(Assembler::Zero, Imm32(AsmJSStackAlignment - 1), &ok);
     masm.breakpoint();
     masm.bind(&ok);
 #endif
 
-    MWasmCall::Callee callee = mir->callee();
+    wasm::CalleeDesc callee = mir->callee();
     switch (callee.which()) {
-      case MWasmCall::Callee::Internal: {
+      case wasm::CalleeDesc::Internal: {
         masm.call(mir->desc(), callee.internalFuncIndex());
         break;
       }
-      case MWasmCall::Callee::Import: {
+      case wasm::CalleeDesc::Import: {
         // Load the callee, before the caller's registers are clobbered.
         uint32_t globalDataOffset = callee.importGlobalDataOffset();;
         masm.loadWasmGlobalPtr(globalDataOffset + offsetof(wasm::FuncImportTls, code), ABINonArgReg0);
 
         // Save the caller's TLS register in a reserved stack slot (below the
         // call's stack arguments) for retrieval after the call.
         masm.storePtr(WasmTlsReg, Address(masm.getStackPointer(), callee.importTlsStackOffset()));
 
@@ -1528,17 +1528,17 @@ CodeGeneratorShared::emitWasmCallBase(LW
         masm.loadWasmPinnedRegsFromTls();
         masm.call(mir->desc(), ABINonArgReg0);
 
         // After return, restore the caller's TLS and pinned registers.
         masm.loadPtr(Address(masm.getStackPointer(), callee.importTlsStackOffset()), WasmTlsReg);
         masm.loadWasmPinnedRegsFromTls();
         break;
       }
-      case MWasmCall::Callee::WasmTable: {
+      case wasm::CalleeDesc::WasmTable: {
         // Write the sig-id into the ABI sig-id register.
         wasm::SigIdDesc sigId = callee.wasmTableSigId();
         switch (sigId.kind()) {
           case wasm::SigIdDesc::Kind::Global:
             masm.loadWasmGlobalPtr(sigId.globalDataOffset(), WasmTableCallSigReg);
             break;
           case wasm::SigIdDesc::Kind::Immediate:
             masm.move32(Imm32(sigId.immediate()), WasmTableCallSigReg);
@@ -1550,24 +1550,24 @@ CodeGeneratorShared::emitWasmCallBase(LW
         // The index is passed in WasmTableCallIndexReg. WebAssembly throws if the index is
         // out-of-bounds.
         masm.branch32(Assembler::Condition::AboveOrEqual,
                       WasmTableCallIndexReg, Imm32(callee.wasmTableLength()),
                       wasm::JumpTarget::OutOfBounds);
 
         MOZ_FALLTHROUGH;
       }
-      case MWasmCall::Callee::AsmJSTable: {
+      case wasm::CalleeDesc::AsmJSTable: {
         masm.loadWasmGlobalPtr(callee.tableGlobalDataOffset(), WasmTableCallScratchReg);
         masm.loadPtr(BaseIndex(WasmTableCallScratchReg, WasmTableCallIndexReg, ScalePointer),
                      WasmTableCallScratchReg);
         masm.call(mir->desc(), WasmTableCallScratchReg);
         break;
       }
-      case MWasmCall::Callee::Builtin: {
+      case wasm::CalleeDesc::Builtin: {
         masm.call(callee.builtin());
         break;
       }
     }
 
     if (mir->spIncrement())
         masm.reserveStack(mir->spIncrement());
 }
--- a/js/src/jit/x86/CodeGenerator-x86.cpp
+++ b/js/src/jit/x86/CodeGenerator-x86.cpp
@@ -397,17 +397,17 @@ CodeGeneratorX86::visitLoadTypedArrayEle
 void
 CodeGeneratorX86::emitWasmCall(LWasmCallBase* ins)
 {
     MWasmCall* mir = ins->mir();
 
     emitWasmCallBase(ins);
 
     if (IsFloatingPointType(mir->type()) &&
-        mir->callee().which() == MWasmCall::Callee::Builtin)
+        mir->callee().which() == wasm::CalleeDesc::Builtin)
     {
         if (mir->type() == MIRType::Float32) {
             masm.reserveStack(sizeof(float));
             Operand op(esp, 0);
             masm.fstp32(op);
             masm.loadFloat32(op, ReturnFloat32Reg);
             masm.freeStack(sizeof(float));
         } else {