Example #1
0
MCSymbol *WebAssemblyMCInstLower::GetExternalSymbolSymbol(
    const MachineOperand &MO) const {
  const char *Name = MO.getSymbolName();
  MCSymbol *Sym = Printer.GetExternalSymbolSymbol(Name);
  if (isa<MCSymbolELF>(Sym))
    return Sym;

  MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Sym);
  const WebAssemblySubtarget &Subtarget = Printer.getSubtarget();

  // __stack_pointer is a global variable; all other external symbols used by
  // CodeGen are functions.
  if (strcmp(Name, "__stack_pointer") == 0)
    return WasmSym;

  SmallVector<wasm::ValType, 4> Returns;
  SmallVector<wasm::ValType, 4> Params;
  GetSignature(Subtarget, Name, Returns, Params);

  WasmSym->setReturns(std::move(Returns));
  WasmSym->setParams(std::move(Params));
  WasmSym->setIsFunction(true);

  return WasmSym;
}
Example #2
0
MCSymbol *
WebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const {
  const GlobalValue *Global = MO.getGlobal();
  MCSymbol *Sym = Printer.getSymbol(Global);
  if (isa<MCSymbolELF>(Sym))
    return Sym;

  MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Sym);

  if (const auto *FuncTy = dyn_cast<FunctionType>(Global->getValueType())) {
    const MachineFunction &MF = *MO.getParent()->getParent()->getParent();
    const TargetMachine &TM = MF.getTarget();
    const Function &CurrentFunc = *MF.getFunction();

    SmallVector<wasm::ValType, 4> Returns;
    SmallVector<wasm::ValType, 4> Params;

    wasm::ValType iPTR =
        MF.getSubtarget<WebAssemblySubtarget>().hasAddr64() ?
        wasm::ValType::I64 :
        wasm::ValType::I32;

    SmallVector<MVT, 4> ResultMVTs;
    ComputeLegalValueVTs(CurrentFunc, TM, FuncTy->getReturnType(), ResultMVTs);
    // WebAssembly can't currently handle returning tuples.
    if (ResultMVTs.size() <= 1)
      for (MVT ResultMVT : ResultMVTs)
        Returns.push_back(WebAssembly::toValType(ResultMVT));
    else
      Params.push_back(iPTR);

    for (Type *Ty : FuncTy->params()) {
      SmallVector<MVT, 4> ParamMVTs;
      ComputeLegalValueVTs(CurrentFunc, TM, Ty, ParamMVTs);
      for (MVT ParamMVT : ParamMVTs)
        Params.push_back(WebAssembly::toValType(ParamMVT));
    }

    if (FuncTy->isVarArg())
      Params.push_back(iPTR);

    WasmSym->setReturns(std::move(Returns));
    WasmSym->setParams(std::move(Params));
    WasmSym->setIsFunction(true);
  }

  return WasmSym;
}
MCSymbol *WebAssemblyMCInstLower::GetExternalSymbolSymbol(
    const MachineOperand &MO) const {
  const char *Name = MO.getSymbolName();
  MCSymbolWasm *WasmSym =
      cast<MCSymbolWasm>(Printer.GetExternalSymbolSymbol(Name));
  const WebAssemblySubtarget &Subtarget = Printer.getSubtarget();

  // Except for the two exceptions (__stack_pointer and __cpp_exception), all
  // other external symbols used by CodeGen are functions. It's OK to hardcode
  // knowledge of specific symbols here; this method is precisely there for
  // fetching the signatures of known Clang-provided symbols.
  if (strcmp(Name, "__stack_pointer") == 0) {
    WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL);
    WasmSym->setGlobalType(wasm::WasmGlobalType{
        uint8_t(Subtarget.hasAddr64() ? wasm::WASM_TYPE_I64
                                      : wasm::WASM_TYPE_I32),
        true});
    return WasmSym;
  }

  SmallVector<wasm::ValType, 4> Returns;
  SmallVector<wasm::ValType, 4> Params;
  if (strcmp(Name, "__cpp_exception") == 0) {
    WasmSym->setType(wasm::WASM_SYMBOL_TYPE_EVENT);
    // We can't confirm its signature index for now because there can be
    // imported exceptions. Set it to be 0 for now.
    WasmSym->setEventType(
        {wasm::WASM_EVENT_ATTRIBUTE_EXCEPTION, /* SigIndex */ 0});
    // We may have multiple C++ compilation units to be linked together, each of
    // which defines the exception symbol. To resolve them, we declare them as
    // weak.
    WasmSym->setWeak(true);
    WasmSym->setExternal(true);

    // All C++ exceptions are assumed to have a single i32 (for wasm32) or i64
    // (for wasm64) param type and void return type. The reaon is, all C++
    // exception values are pointers, and to share the type section with
    // functions, exceptions are assumed to have void return type.
    Params.push_back(Subtarget.hasAddr64() ? wasm::ValType::I64
                                           : wasm::ValType::I32);
  } else { // Function symbols
    WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
    GetLibcallSignature(Subtarget, Name, Returns, Params);
  }
  auto Signature =
      make_unique<wasm::WasmSignature>(std::move(Returns), std::move(Params));
  WasmSym->setSignature(Signature.get());
  Printer.addSignature(std::move(Signature));

  return WasmSym;
}
void WebAssemblyTargetWasmStreamer::emitIndirectFunctionType(
    MCSymbol *Symbol, SmallVectorImpl<MVT> &Params,
    SmallVectorImpl<MVT> &Results) {
  MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Symbol);
  if (WasmSym->isFunction()) {
    // Symbol already has its arguments and result set.
    return;
  }

  SmallVector<wasm::ValType, 4> ValParams;
  for (MVT Ty : Params)
    ValParams.push_back(WebAssembly::toValType(Ty));

  SmallVector<wasm::ValType, 1> ValResults;
  for (MVT Ty : Results)
    ValResults.push_back(WebAssembly::toValType(Ty));

  WasmSym->setParams(std::move(ValParams));
  WasmSym->setReturns(std::move(ValResults));
  WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
}
MCSymbol *
WebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const {
  const GlobalValue *Global = MO.getGlobal();
  MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Printer.getSymbol(Global));

  if (const auto *FuncTy = dyn_cast<FunctionType>(Global->getValueType())) {
    const MachineFunction &MF = *MO.getParent()->getParent()->getParent();
    const TargetMachine &TM = MF.getTarget();
    const Function &CurrentFunc = MF.getFunction();

    SmallVector<MVT, 1> ResultMVTs;
    SmallVector<MVT, 4> ParamMVTs;
    ComputeSignatureVTs(FuncTy, CurrentFunc, TM, ParamMVTs, ResultMVTs);

    auto Signature = SignatureFromMVTs(ResultMVTs, ParamMVTs);
    WasmSym->setSignature(Signature.get());
    Printer.addSignature(std::move(Signature));
    WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
  }

  return WasmSym;
}
Example #6
0
void WebAssemblyMCInstLower::Lower(const MachineInstr *MI,
                                   MCInst &OutMI) const {
  OutMI.setOpcode(MI->getOpcode());

  const MCInstrDesc &Desc = MI->getDesc();
  for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
    const MachineOperand &MO = MI->getOperand(i);

    MCOperand MCOp;
    switch (MO.getType()) {
    default:
      MI->print(errs());
      llvm_unreachable("unknown operand type");
    case MachineOperand::MO_MachineBasicBlock:
      MI->print(errs());
      llvm_unreachable("MachineBasicBlock operand should have been rewritten");
    case MachineOperand::MO_Register: {
      // Ignore all implicit register operands.
      if (MO.isImplicit())
        continue;
      const WebAssemblyFunctionInfo &MFI =
          *MI->getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>();
      unsigned WAReg = MFI.getWAReg(MO.getReg());
      MCOp = MCOperand::createReg(WAReg);
      break;
    }
    case MachineOperand::MO_Immediate:
      if (i < Desc.NumOperands) {
        const MCOperandInfo &Info = Desc.OpInfo[i];
        if (Info.OperandType == WebAssembly::OPERAND_TYPEINDEX) {
          MCSymbol *Sym = Printer.createTempSymbol("typeindex");
          if (!isa<MCSymbolELF>(Sym)) {
            SmallVector<wasm::ValType, 4> Returns;
            SmallVector<wasm::ValType, 4> Params;

            const MachineRegisterInfo &MRI =
                MI->getParent()->getParent()->getRegInfo();
            for (const MachineOperand &MO : MI->defs())
              Returns.push_back(getType(MRI.getRegClass(MO.getReg())));
            for (const MachineOperand &MO : MI->explicit_uses())
              if (MO.isReg())
                Params.push_back(getType(MRI.getRegClass(MO.getReg())));

            // call_indirect instructions have a callee operand at the end which
            // doesn't count as a param.
            if (WebAssembly::isCallIndirect(*MI))
              Params.pop_back();

            MCSymbolWasm *WasmSym = cast<MCSymbolWasm>(Sym);
            WasmSym->setReturns(std::move(Returns));
            WasmSym->setParams(std::move(Params));
            WasmSym->setIsFunction(true);

            const MCExpr *Expr =
                MCSymbolRefExpr::create(WasmSym,
                                        MCSymbolRefExpr::VK_WebAssembly_TYPEINDEX,
                                        Ctx);
            MCOp = MCOperand::createExpr(Expr);
            break;
          }
        }
      }
      MCOp = MCOperand::createImm(MO.getImm());
      break;
    case MachineOperand::MO_FPImmediate: {
      // TODO: MC converts all floating point immediate operands to double.
      // This is fine for numeric values, but may cause NaNs to change bits.
      const ConstantFP *Imm = MO.getFPImm();
      if (Imm->getType()->isFloatTy())
        MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToFloat());
      else if (Imm->getType()->isDoubleTy())
        MCOp = MCOperand::createFPImm(Imm->getValueAPF().convertToDouble());
      else
        llvm_unreachable("unknown floating point immediate type");
      break;
    }
    case MachineOperand::MO_GlobalAddress:
      assert(MO.getTargetFlags() == 0 &&
             "WebAssembly does not use target flags on GlobalAddresses");
      MCOp = LowerSymbolOperand(GetGlobalAddressSymbol(MO), MO.getOffset(),
                                MO.getGlobal()->getValueType()->isFunctionTy());
      break;
    case MachineOperand::MO_ExternalSymbol:
      // The target flag indicates whether this is a symbol for a
      // variable or a function.
      assert((MO.getTargetFlags() & -2) == 0 &&
             "WebAssembly uses only one target flag bit on ExternalSymbols");
      MCOp = LowerSymbolOperand(GetExternalSymbolSymbol(MO), /*Offset=*/0,
                                MO.getTargetFlags() & 1);
      break;
    }

    OutMI.addOperand(MCOp);
  }
}