2012-04-12 19:55:53 +02:00
|
|
|
//===- HexagonMCInstLower.cpp - Convert Hexagon MachineInstr to an MCInst -===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-04-12 19:55:53 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains code to lower Hexagon MachineInstrs to their corresponding
|
|
|
|
// MCInst records.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Hexagon.h"
|
|
|
|
#include "HexagonAsmPrinter.h"
|
2017-08-01 23:20:10 +02:00
|
|
|
#include "MCTargetDesc/HexagonMCExpr.h"
|
2015-05-29 16:44:13 +02:00
|
|
|
#include "MCTargetDesc/HexagonMCInstrInfo.h"
|
2017-08-01 23:20:10 +02:00
|
|
|
#include "MCTargetDesc/HexagonMCTargetDesc.h"
|
|
|
|
#include "llvm/ADT/APFloat.h"
|
|
|
|
#include "llvm/ADT/APInt.h"
|
2012-04-12 19:55:53 +02:00
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
2017-08-01 23:20:10 +02:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
|
|
|
#include "llvm/CodeGen/MachineOperand.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/Constants.h"
|
2015-05-29 16:44:13 +02:00
|
|
|
#include "llvm/MC/MCContext.h"
|
2012-04-12 19:55:53 +02:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
|
|
|
#include "llvm/MC/MCInst.h"
|
2017-08-01 23:20:10 +02:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <cassert>
|
2012-04-12 19:55:53 +02:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2015-12-03 00:08:29 +01:00
|
|
|
namespace llvm {
|
2017-08-01 23:20:10 +02:00
|
|
|
|
|
|
|
void HexagonLowerToMC(const MCInstrInfo &MCII, const MachineInstr *MI,
|
|
|
|
MCInst &MCB, HexagonAsmPrinter &AP);
|
|
|
|
|
|
|
|
} // end namespace llvm
|
2015-12-03 00:08:29 +01:00
|
|
|
|
|
|
|
static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol,
|
2016-02-29 19:39:51 +01:00
|
|
|
HexagonAsmPrinter &Printer, bool MustExtend) {
|
2012-04-12 19:55:53 +02:00
|
|
|
MCContext &MC = Printer.OutContext;
|
|
|
|
const MCExpr *ME;
|
|
|
|
|
2015-12-03 00:08:29 +01:00
|
|
|
// Populate the relocation type based on Hexagon target flags
|
|
|
|
// set on an operand
|
|
|
|
MCSymbolRefExpr::VariantKind RelocationType;
|
2017-05-02 20:15:33 +02:00
|
|
|
switch (MO.getTargetFlags() & ~HexagonII::HMOTF_ConstExtended) {
|
2015-12-03 00:08:29 +01:00
|
|
|
default:
|
|
|
|
RelocationType = MCSymbolRefExpr::VK_None;
|
|
|
|
break;
|
|
|
|
case HexagonII::MO_PCREL:
|
2020-02-19 16:47:14 +01:00
|
|
|
RelocationType = MCSymbolRefExpr::VK_PCREL;
|
2015-12-03 00:08:29 +01:00
|
|
|
break;
|
|
|
|
case HexagonII::MO_GOT:
|
|
|
|
RelocationType = MCSymbolRefExpr::VK_GOT;
|
|
|
|
break;
|
|
|
|
case HexagonII::MO_LO16:
|
|
|
|
RelocationType = MCSymbolRefExpr::VK_Hexagon_LO16;
|
|
|
|
break;
|
|
|
|
case HexagonII::MO_HI16:
|
|
|
|
RelocationType = MCSymbolRefExpr::VK_Hexagon_HI16;
|
|
|
|
break;
|
|
|
|
case HexagonII::MO_GPREL:
|
|
|
|
RelocationType = MCSymbolRefExpr::VK_Hexagon_GPREL;
|
|
|
|
break;
|
2016-02-18 16:42:57 +01:00
|
|
|
case HexagonII::MO_GDGOT:
|
|
|
|
RelocationType = MCSymbolRefExpr::VK_Hexagon_GD_GOT;
|
|
|
|
break;
|
|
|
|
case HexagonII::MO_GDPLT:
|
|
|
|
RelocationType = MCSymbolRefExpr::VK_Hexagon_GD_PLT;
|
|
|
|
break;
|
|
|
|
case HexagonII::MO_IE:
|
|
|
|
RelocationType = MCSymbolRefExpr::VK_Hexagon_IE;
|
|
|
|
break;
|
|
|
|
case HexagonII::MO_IEGOT:
|
|
|
|
RelocationType = MCSymbolRefExpr::VK_Hexagon_IE_GOT;
|
|
|
|
break;
|
|
|
|
case HexagonII::MO_TPREL:
|
|
|
|
RelocationType = MCSymbolRefExpr::VK_TPREL;
|
|
|
|
break;
|
2015-12-03 00:08:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ME = MCSymbolRefExpr::create(Symbol, RelocationType, MC);
|
2012-04-12 19:55:53 +02:00
|
|
|
|
|
|
|
if (!MO.isJTI() && MO.getOffset())
|
2015-05-30 03:25:56 +02:00
|
|
|
ME = MCBinaryExpr::createAdd(ME, MCConstantExpr::create(MO.getOffset(), MC),
|
2012-04-12 19:55:53 +02:00
|
|
|
MC);
|
|
|
|
|
2016-02-29 19:39:51 +01:00
|
|
|
ME = HexagonMCExpr::create(ME, MC);
|
|
|
|
HexagonMCInstrInfo::setMustExtend(*ME, MustExtend);
|
|
|
|
return MCOperand::createExpr(ME);
|
2012-04-12 19:55:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create an MCInst from a MachineInstr
|
2015-12-03 00:08:29 +01:00
|
|
|
void llvm::HexagonLowerToMC(const MCInstrInfo &MCII, const MachineInstr *MI,
|
|
|
|
MCInst &MCB, HexagonAsmPrinter &AP) {
|
|
|
|
if (MI->getOpcode() == Hexagon::ENDLOOP0) {
|
2015-05-29 16:44:13 +02:00
|
|
|
HexagonMCInstrInfo::setInnerLoop(MCB);
|
|
|
|
return;
|
|
|
|
}
|
2015-12-03 00:08:29 +01:00
|
|
|
if (MI->getOpcode() == Hexagon::ENDLOOP1) {
|
2015-05-29 16:44:13 +02:00
|
|
|
HexagonMCInstrInfo::setOuterLoop(MCB);
|
|
|
|
return;
|
|
|
|
}
|
2015-12-03 00:08:29 +01:00
|
|
|
MCInst *MCI = new (AP.OutContext) MCInst;
|
2015-05-29 16:44:13 +02:00
|
|
|
MCI->setOpcode(MI->getOpcode());
|
|
|
|
assert(MCI->getOpcode() == static_cast<unsigned>(MI->getOpcode()) &&
|
|
|
|
"MCI opcode should have been set on construction");
|
2012-04-12 19:55:53 +02:00
|
|
|
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i < e; i++) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
|
|
|
MCOperand MCO;
|
2016-02-29 19:39:51 +01:00
|
|
|
bool MustExtend = MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended;
|
2012-04-12 19:55:53 +02:00
|
|
|
|
|
|
|
switch (MO.getType()) {
|
|
|
|
default:
|
2017-01-28 03:02:38 +01:00
|
|
|
MI->print(errs());
|
2012-05-03 23:52:53 +02:00
|
|
|
llvm_unreachable("unknown operand type");
|
2017-02-17 23:14:51 +01:00
|
|
|
case MachineOperand::MO_RegisterMask:
|
|
|
|
continue;
|
2012-04-12 19:55:53 +02:00
|
|
|
case MachineOperand::MO_Register:
|
|
|
|
// Ignore all implicit register operands.
|
2017-02-17 23:14:51 +01:00
|
|
|
if (MO.isImplicit())
|
|
|
|
continue;
|
2015-05-13 20:37:00 +02:00
|
|
|
MCO = MCOperand::createReg(MO.getReg());
|
2012-04-12 19:55:53 +02:00
|
|
|
break;
|
|
|
|
case MachineOperand::MO_FPImmediate: {
|
|
|
|
APFloat Val = MO.getFPImm()->getValueAPF();
|
|
|
|
// FP immediates are used only when setting GPRs, so they may be dealt
|
|
|
|
// with like regular immediates from this point on.
|
2016-02-29 19:39:51 +01:00
|
|
|
auto Expr = HexagonMCExpr::create(
|
2017-04-23 14:15:30 +02:00
|
|
|
MCConstantExpr::create(*Val.bitcastToAPInt().getRawData(),
|
2016-02-29 19:39:51 +01:00
|
|
|
AP.OutContext),
|
|
|
|
AP.OutContext);
|
|
|
|
HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
|
|
|
|
MCO = MCOperand::createExpr(Expr);
|
2012-04-12 19:55:53 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-02-29 19:39:51 +01:00
|
|
|
case MachineOperand::MO_Immediate: {
|
|
|
|
auto Expr = HexagonMCExpr::create(
|
|
|
|
MCConstantExpr::create(MO.getImm(), AP.OutContext), AP.OutContext);
|
|
|
|
HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
|
|
|
|
MCO = MCOperand::createExpr(Expr);
|
2012-04-12 19:55:53 +02:00
|
|
|
break;
|
2016-02-29 19:39:51 +01:00
|
|
|
}
|
|
|
|
case MachineOperand::MO_MachineBasicBlock: {
|
|
|
|
MCExpr const *Expr = MCSymbolRefExpr::create(MO.getMBB()->getSymbol(),
|
|
|
|
AP.OutContext);
|
|
|
|
Expr = HexagonMCExpr::create(Expr, AP.OutContext);
|
|
|
|
HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
|
|
|
|
MCO = MCOperand::createExpr(Expr);
|
2012-04-12 19:55:53 +02:00
|
|
|
break;
|
2016-02-29 19:39:51 +01:00
|
|
|
}
|
2012-04-12 19:55:53 +02:00
|
|
|
case MachineOperand::MO_GlobalAddress:
|
2016-02-29 19:39:51 +01:00
|
|
|
MCO = GetSymbolRef(MO, AP.getSymbol(MO.getGlobal()), AP, MustExtend);
|
2012-04-12 19:55:53 +02:00
|
|
|
break;
|
|
|
|
case MachineOperand::MO_ExternalSymbol:
|
2014-11-04 01:14:36 +01:00
|
|
|
MCO = GetSymbolRef(MO, AP.GetExternalSymbolSymbol(MO.getSymbolName()),
|
2016-02-29 19:39:51 +01:00
|
|
|
AP, MustExtend);
|
2012-04-12 19:55:53 +02:00
|
|
|
break;
|
|
|
|
case MachineOperand::MO_JumpTableIndex:
|
2016-02-29 19:39:51 +01:00
|
|
|
MCO = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP, MustExtend);
|
2012-04-12 19:55:53 +02:00
|
|
|
break;
|
|
|
|
case MachineOperand::MO_ConstantPoolIndex:
|
2016-02-29 19:39:51 +01:00
|
|
|
MCO = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP, MustExtend);
|
2012-04-12 19:55:53 +02:00
|
|
|
break;
|
|
|
|
case MachineOperand::MO_BlockAddress:
|
2016-02-29 19:39:51 +01:00
|
|
|
MCO = GetSymbolRef(MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()), AP,
|
|
|
|
MustExtend);
|
2012-04-12 19:55:53 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-05-29 16:44:13 +02:00
|
|
|
MCI->addOperand(MCO);
|
2012-04-12 19:55:53 +02:00
|
|
|
}
|
2015-12-15 18:05:45 +01:00
|
|
|
AP.HexagonProcessInstruction(*MCI, *MI);
|
2016-02-29 19:39:51 +01:00
|
|
|
HexagonMCInstrInfo::extendIfNeeded(AP.OutContext, MCII, MCB, *MCI);
|
2015-05-29 16:44:13 +02:00
|
|
|
MCB.addOperand(MCOperand::createInst(MCI));
|
2012-04-12 19:55:53 +02:00
|
|
|
}
|