2012-04-13 01:55:53 +08:00
|
|
|
//===- HexagonMCInstLower.cpp - Convert Hexagon MachineInstr to an MCInst -===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains code to lower Hexagon MachineInstrs to their corresponding
|
|
|
|
// MCInst records.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Hexagon.h"
|
|
|
|
#include "HexagonAsmPrinter.h"
|
|
|
|
#include "HexagonMachineFunctionInfo.h"
|
2015-05-29 22:44:13 +08:00
|
|
|
#include "MCTargetDesc/HexagonMCInstrInfo.h"
|
|
|
|
|
2012-04-13 01:55:53 +08:00
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
2014-01-08 05:19:40 +08:00
|
|
|
#include "llvm/IR/Mangler.h"
|
2015-05-29 22:44:13 +08:00
|
|
|
#include "llvm/MC/MCContext.h"
|
2012-04-13 01:55:53 +08:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
|
|
|
#include "llvm/MC/MCInst.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2015-12-03 07:08:29 +08:00
|
|
|
namespace llvm {
|
|
|
|
void HexagonLowerToMC(const MCInstrInfo &MCII, const MachineInstr *MI,
|
|
|
|
MCInst &MCB, HexagonAsmPrinter &AP);
|
|
|
|
}
|
|
|
|
|
|
|
|
static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol,
|
2016-03-01 02:39:51 +08:00
|
|
|
HexagonAsmPrinter &Printer, bool MustExtend) {
|
2012-04-13 01:55:53 +08:00
|
|
|
MCContext &MC = Printer.OutContext;
|
|
|
|
const MCExpr *ME;
|
|
|
|
|
2015-12-03 07:08:29 +08:00
|
|
|
// Populate the relocation type based on Hexagon target flags
|
|
|
|
// set on an operand
|
|
|
|
MCSymbolRefExpr::VariantKind RelocationType;
|
|
|
|
switch (MO.getTargetFlags()) {
|
|
|
|
default:
|
|
|
|
RelocationType = MCSymbolRefExpr::VK_None;
|
|
|
|
break;
|
|
|
|
case HexagonII::MO_PCREL:
|
|
|
|
RelocationType = MCSymbolRefExpr::VK_Hexagon_PCREL;
|
|
|
|
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 23:42:57 +08: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 07:08:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ME = MCSymbolRefExpr::create(Symbol, RelocationType, MC);
|
2012-04-13 01:55:53 +08:00
|
|
|
|
|
|
|
if (!MO.isJTI() && MO.getOffset())
|
2015-05-30 09:25:56 +08:00
|
|
|
ME = MCBinaryExpr::createAdd(ME, MCConstantExpr::create(MO.getOffset(), MC),
|
2012-04-13 01:55:53 +08:00
|
|
|
MC);
|
|
|
|
|
2016-03-01 02:39:51 +08:00
|
|
|
ME = HexagonMCExpr::create(ME, MC);
|
|
|
|
HexagonMCInstrInfo::setMustExtend(*ME, MustExtend);
|
|
|
|
return MCOperand::createExpr(ME);
|
2012-04-13 01:55:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create an MCInst from a MachineInstr
|
2015-12-03 07:08:29 +08:00
|
|
|
void llvm::HexagonLowerToMC(const MCInstrInfo &MCII, const MachineInstr *MI,
|
|
|
|
MCInst &MCB, HexagonAsmPrinter &AP) {
|
|
|
|
if (MI->getOpcode() == Hexagon::ENDLOOP0) {
|
2015-05-29 22:44:13 +08:00
|
|
|
HexagonMCInstrInfo::setInnerLoop(MCB);
|
|
|
|
return;
|
|
|
|
}
|
2015-12-03 07:08:29 +08:00
|
|
|
if (MI->getOpcode() == Hexagon::ENDLOOP1) {
|
2015-05-29 22:44:13 +08:00
|
|
|
HexagonMCInstrInfo::setOuterLoop(MCB);
|
|
|
|
return;
|
|
|
|
}
|
2015-12-03 07:08:29 +08:00
|
|
|
MCInst *MCI = new (AP.OutContext) MCInst;
|
2015-05-29 22:44:13 +08:00
|
|
|
MCI->setOpcode(MI->getOpcode());
|
|
|
|
assert(MCI->getOpcode() == static_cast<unsigned>(MI->getOpcode()) &&
|
|
|
|
"MCI opcode should have been set on construction");
|
2012-04-13 01:55:53 +08:00
|
|
|
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i < e; i++) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
|
|
|
MCOperand MCO;
|
2016-03-01 02:39:51 +08:00
|
|
|
bool MustExtend = MO.getTargetFlags() & HexagonII::HMOTF_ConstExtended;
|
2012-04-13 01:55:53 +08:00
|
|
|
|
|
|
|
switch (MO.getType()) {
|
|
|
|
default:
|
2017-01-28 10:02:38 +08:00
|
|
|
MI->print(errs());
|
2012-05-04 05:52:53 +08:00
|
|
|
llvm_unreachable("unknown operand type");
|
2017-02-18 06:14:51 +08:00
|
|
|
case MachineOperand::MO_RegisterMask:
|
|
|
|
continue;
|
2012-04-13 01:55:53 +08:00
|
|
|
case MachineOperand::MO_Register:
|
|
|
|
// Ignore all implicit register operands.
|
2017-02-18 06:14:51 +08:00
|
|
|
if (MO.isImplicit())
|
|
|
|
continue;
|
2015-05-14 02:37:00 +08:00
|
|
|
MCO = MCOperand::createReg(MO.getReg());
|
2012-04-13 01:55:53 +08: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-03-01 02:39:51 +08:00
|
|
|
auto Expr = HexagonMCExpr::create(
|
2017-04-23 14:41:11 +08:00
|
|
|
MCConstantExpr::create(Val.bitcastToAPInt().getZExtValue(),
|
2016-03-01 02:39:51 +08:00
|
|
|
AP.OutContext),
|
|
|
|
AP.OutContext);
|
|
|
|
HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
|
|
|
|
MCO = MCOperand::createExpr(Expr);
|
2012-04-13 01:55:53 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-03-01 02:39:51 +08: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-13 01:55:53 +08:00
|
|
|
break;
|
2016-03-01 02:39:51 +08: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-13 01:55:53 +08:00
|
|
|
break;
|
2016-03-01 02:39:51 +08:00
|
|
|
}
|
2012-04-13 01:55:53 +08:00
|
|
|
case MachineOperand::MO_GlobalAddress:
|
2016-03-01 02:39:51 +08:00
|
|
|
MCO = GetSymbolRef(MO, AP.getSymbol(MO.getGlobal()), AP, MustExtend);
|
2012-04-13 01:55:53 +08:00
|
|
|
break;
|
|
|
|
case MachineOperand::MO_ExternalSymbol:
|
2014-11-04 08:14:36 +08:00
|
|
|
MCO = GetSymbolRef(MO, AP.GetExternalSymbolSymbol(MO.getSymbolName()),
|
2016-03-01 02:39:51 +08:00
|
|
|
AP, MustExtend);
|
2012-04-13 01:55:53 +08:00
|
|
|
break;
|
|
|
|
case MachineOperand::MO_JumpTableIndex:
|
2016-03-01 02:39:51 +08:00
|
|
|
MCO = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP, MustExtend);
|
2012-04-13 01:55:53 +08:00
|
|
|
break;
|
|
|
|
case MachineOperand::MO_ConstantPoolIndex:
|
2016-03-01 02:39:51 +08:00
|
|
|
MCO = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP, MustExtend);
|
2012-04-13 01:55:53 +08:00
|
|
|
break;
|
|
|
|
case MachineOperand::MO_BlockAddress:
|
2016-03-01 02:39:51 +08:00
|
|
|
MCO = GetSymbolRef(MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()), AP,
|
|
|
|
MustExtend);
|
2012-04-13 01:55:53 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-05-29 22:44:13 +08:00
|
|
|
MCI->addOperand(MCO);
|
2012-04-13 01:55:53 +08:00
|
|
|
}
|
2015-12-16 01:05:45 +08:00
|
|
|
AP.HexagonProcessInstruction(*MCI, *MI);
|
2016-03-01 02:39:51 +08:00
|
|
|
HexagonMCInstrInfo::extendIfNeeded(AP.OutContext, MCII, MCB, *MCI);
|
2015-05-29 22:44:13 +08:00
|
|
|
MCB.addOperand(MCOperand::createInst(MCI));
|
2012-04-13 01:55:53 +08:00
|
|
|
}
|