2010-11-15 03:53:02 +08:00
|
|
|
//===-- PPCMCInstLower.cpp - Convert PPC MachineInstr to an MCInst --------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08: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
|
2010-11-15 03:53:02 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains code to lower PPC MachineInstrs to their corresponding
|
|
|
|
// MCInst records.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-05-24 06:26:41 +08:00
|
|
|
#include "MCTargetDesc/PPCMCExpr.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "PPC.h"
|
2015-01-14 19:23:27 +08:00
|
|
|
#include "PPCSubtarget.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2013-04-27 08:43:16 +08:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2010-11-15 03:53:02 +08:00
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
2010-11-15 10:46:57 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2010-11-15 07:42:06 +08:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfoImpls.h"
|
2017-11-17 09:07:10 +08:00
|
|
|
#include "llvm/CodeGen/TargetLowering.h"
|
2014-01-04 03:21:54 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2013-02-20 08:31:54 +08:00
|
|
|
#include "llvm/IR/GlobalValue.h"
|
2014-01-08 05:19:40 +08:00
|
|
|
#include "llvm/IR/Mangler.h"
|
2010-11-15 07:42:06 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2010-11-15 03:53:02 +08:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
|
|
|
#include "llvm/MC/MCInst.h"
|
2018-03-24 07:58:19 +08:00
|
|
|
#include "llvm/Target/TargetLoweringObjectFile.h"
|
2010-11-15 03:53:02 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2016-09-17 01:07:13 +08:00
|
|
|
static MCSymbol *GetSymbolFromOperand(const MachineOperand &MO,
|
|
|
|
AsmPrinter &AP) {
|
2014-02-20 01:23:20 +08:00
|
|
|
const TargetMachine &TM = AP.TM;
|
2016-09-17 01:07:13 +08:00
|
|
|
Mangler &Mang = TM.getObjFileLowering()->getMangler();
|
2015-07-16 14:11:10 +08:00
|
|
|
const DataLayout &DL = AP.getDataLayout();
|
2010-11-15 07:42:06 +08:00
|
|
|
MCContext &Ctx = AP.OutContext;
|
|
|
|
|
|
|
|
SmallString<128> Name;
|
|
|
|
if (!MO.isGlobal()) {
|
|
|
|
assert(MO.isSymbol() && "Isn't a symbol reference");
|
2015-07-16 14:11:10 +08:00
|
|
|
Mangler::getNameWithPrefix(Name, MO.getSymbolName(), DL);
|
2013-12-02 11:26:43 +08:00
|
|
|
} else {
|
2010-11-15 07:42:06 +08:00
|
|
|
const GlobalValue *GV = MO.getGlobal();
|
2016-09-17 01:07:13 +08:00
|
|
|
TM.getNameWithPrefix(Name, GV, Mang);
|
2010-11-15 07:42:06 +08:00
|
|
|
}
|
2013-12-02 11:26:43 +08:00
|
|
|
|
2015-05-19 02:43:14 +08:00
|
|
|
MCSymbol *Sym = Ctx.getOrCreateSymbol(Name);
|
2010-11-15 11:13:19 +08:00
|
|
|
|
2013-12-02 11:26:43 +08:00
|
|
|
return Sym;
|
2010-11-15 07:42:06 +08:00
|
|
|
}
|
|
|
|
|
2010-11-15 05:20:46 +08:00
|
|
|
static MCOperand GetSymbolRef(const MachineOperand &MO, const MCSymbol *Symbol,
|
2019-10-06 22:44:22 +08:00
|
|
|
AsmPrinter &Printer, bool IsDarwin) {
|
2010-11-15 05:20:46 +08:00
|
|
|
MCContext &Ctx = Printer.OutContext;
|
2010-11-15 07:42:06 +08:00
|
|
|
MCSymbolRefExpr::VariantKind RefKind = MCSymbolRefExpr::VK_None;
|
|
|
|
|
2012-06-05 01:36:38 +08:00
|
|
|
unsigned access = MO.getTargetFlags() & PPCII::MO_ACCESS_MASK;
|
|
|
|
|
2013-06-21 00:23:52 +08:00
|
|
|
switch (access) {
|
2013-06-21 22:42:20 +08:00
|
|
|
case PPCII::MO_TPREL_LO:
|
|
|
|
RefKind = MCSymbolRefExpr::VK_PPC_TPREL_LO;
|
2013-06-21 00:23:52 +08:00
|
|
|
break;
|
2013-06-21 22:42:20 +08:00
|
|
|
case PPCII::MO_TPREL_HA:
|
|
|
|
RefKind = MCSymbolRefExpr::VK_PPC_TPREL_HA;
|
2013-06-21 00:23:52 +08:00
|
|
|
break;
|
2013-06-21 22:42:20 +08:00
|
|
|
case PPCII::MO_DTPREL_LO:
|
|
|
|
RefKind = MCSymbolRefExpr::VK_PPC_DTPREL_LO;
|
2013-06-21 00:23:52 +08:00
|
|
|
break;
|
2013-06-21 22:42:20 +08:00
|
|
|
case PPCII::MO_TLSLD_LO:
|
|
|
|
RefKind = MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO;
|
2013-06-21 00:23:52 +08:00
|
|
|
break;
|
2013-06-21 22:42:20 +08:00
|
|
|
case PPCII::MO_TOC_LO:
|
|
|
|
RefKind = MCSymbolRefExpr::VK_PPC_TOC_LO;
|
2013-06-21 00:23:52 +08:00
|
|
|
break;
|
2013-07-05 20:22:36 +08:00
|
|
|
case PPCII::MO_TLS:
|
|
|
|
RefKind = MCSymbolRefExpr::VK_PPC_TLS;
|
|
|
|
break;
|
2013-05-24 06:26:41 +08:00
|
|
|
}
|
2010-11-15 07:42:06 +08:00
|
|
|
|
2018-03-27 19:23:53 +08:00
|
|
|
if (MO.getTargetFlags() == PPCII::MO_PLT)
|
2014-07-19 07:29:49 +08:00
|
|
|
RefKind = MCSymbolRefExpr::VK_PLT;
|
|
|
|
|
2018-03-27 19:23:53 +08:00
|
|
|
const MachineFunction *MF = MO.getParent()->getParent()->getParent();
|
[PPC32] Support PLT calls for -msecure-plt -fpic
Summary:
In Secure PLT ABI, -fpic is similar to -fPIC. The differences are that:
* -fpic stores the address of _GLOBAL_OFFSET_TABLE_ in r30, while -fPIC stores .got2+0x8000.
* -fpic uses an addend of 0 for R_PPC_PLTREL24, while -fPIC uses 0x8000.
Reviewers: hfinkel, jhibbits, joerg, nemanjai, spetrovic
Reviewed By: jhibbits
Subscribers: adalava, kbarton, jsji, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D63563
llvm-svn: 364324
2019-06-25 23:56:32 +08:00
|
|
|
const Module *M = MF->getFunction().getParent();
|
2018-03-27 19:23:53 +08:00
|
|
|
const PPCSubtarget *Subtarget = &(MF->getSubtarget<PPCSubtarget>());
|
|
|
|
const TargetMachine &TM = Printer.TM;
|
2015-05-30 09:25:56 +08:00
|
|
|
const MCExpr *Expr = MCSymbolRefExpr::create(Symbol, RefKind, Ctx);
|
[PPC32] Support PLT calls for -msecure-plt -fpic
Summary:
In Secure PLT ABI, -fpic is similar to -fPIC. The differences are that:
* -fpic stores the address of _GLOBAL_OFFSET_TABLE_ in r30, while -fPIC stores .got2+0x8000.
* -fpic uses an addend of 0 for R_PPC_PLTREL24, while -fPIC uses 0x8000.
Reviewers: hfinkel, jhibbits, joerg, nemanjai, spetrovic
Reviewed By: jhibbits
Subscribers: adalava, kbarton, jsji, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D63563
llvm-svn: 364324
2019-06-25 23:56:32 +08:00
|
|
|
// If -msecure-plt -fPIC, add 32768 to symbol.
|
2018-03-27 19:23:53 +08:00
|
|
|
if (Subtarget->isSecurePlt() && TM.isPositionIndependent() &&
|
[PPC32] Support PLT calls for -msecure-plt -fpic
Summary:
In Secure PLT ABI, -fpic is similar to -fPIC. The differences are that:
* -fpic stores the address of _GLOBAL_OFFSET_TABLE_ in r30, while -fPIC stores .got2+0x8000.
* -fpic uses an addend of 0 for R_PPC_PLTREL24, while -fPIC uses 0x8000.
Reviewers: hfinkel, jhibbits, joerg, nemanjai, spetrovic
Reviewed By: jhibbits
Subscribers: adalava, kbarton, jsji, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D63563
llvm-svn: 364324
2019-06-25 23:56:32 +08:00
|
|
|
M->getPICLevel() == PICLevel::BigPIC &&
|
2018-03-27 19:23:53 +08:00
|
|
|
MO.getTargetFlags() == PPCII::MO_PLT)
|
[PPC32] Support PLT calls for -msecure-plt -fpic
Summary:
In Secure PLT ABI, -fpic is similar to -fPIC. The differences are that:
* -fpic stores the address of _GLOBAL_OFFSET_TABLE_ in r30, while -fPIC stores .got2+0x8000.
* -fpic uses an addend of 0 for R_PPC_PLTREL24, while -fPIC uses 0x8000.
Reviewers: hfinkel, jhibbits, joerg, nemanjai, spetrovic
Reviewed By: jhibbits
Subscribers: adalava, kbarton, jsji, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D63563
llvm-svn: 364324
2019-06-25 23:56:32 +08:00
|
|
|
Expr =
|
|
|
|
MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(32768, Ctx), Ctx);
|
2010-11-15 07:42:06 +08:00
|
|
|
|
2010-11-15 05:20:46 +08:00
|
|
|
if (!MO.isJTI() && MO.getOffset())
|
2015-05-30 09:25:56 +08:00
|
|
|
Expr = MCBinaryExpr::createAdd(Expr,
|
|
|
|
MCConstantExpr::create(MO.getOffset(), Ctx),
|
2010-11-15 05:20:46 +08:00
|
|
|
Ctx);
|
2010-11-15 10:46:57 +08:00
|
|
|
|
2010-11-15 11:13:19 +08:00
|
|
|
// Subtract off the PIC base if required.
|
|
|
|
if (MO.getTargetFlags() & PPCII::MO_PIC_FLAG) {
|
2010-11-15 10:46:57 +08:00
|
|
|
const MachineFunction *MF = MO.getParent()->getParent()->getParent();
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2015-05-30 09:25:56 +08:00
|
|
|
const MCExpr *PB = MCSymbolRefExpr::create(MF->getPICBaseSymbol(), Ctx);
|
|
|
|
Expr = MCBinaryExpr::createSub(Expr, PB, Ctx);
|
2010-11-15 10:46:57 +08:00
|
|
|
}
|
2013-05-24 06:26:41 +08:00
|
|
|
|
2013-06-21 00:23:52 +08:00
|
|
|
// Add ha16() / lo16() markers if required.
|
|
|
|
switch (access) {
|
2013-06-21 22:42:20 +08:00
|
|
|
case PPCII::MO_LO:
|
2019-10-06 22:44:22 +08:00
|
|
|
Expr = PPCMCExpr::createLo(Expr, IsDarwin, Ctx);
|
2013-06-21 00:23:52 +08:00
|
|
|
break;
|
2013-06-21 22:42:20 +08:00
|
|
|
case PPCII::MO_HA:
|
2019-10-06 22:44:22 +08:00
|
|
|
Expr = PPCMCExpr::createHa(Expr, IsDarwin, Ctx);
|
2013-06-21 00:23:52 +08:00
|
|
|
break;
|
2013-05-24 06:26:41 +08:00
|
|
|
}
|
|
|
|
|
2015-05-14 02:37:00 +08:00
|
|
|
return MCOperand::createExpr(Expr);
|
2010-11-15 05:20:46 +08:00
|
|
|
}
|
|
|
|
|
2010-11-15 05:12:33 +08:00
|
|
|
void llvm::LowerPPCMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI,
|
2019-10-06 22:44:22 +08:00
|
|
|
AsmPrinter &AP, bool IsDarwin) {
|
2010-11-15 03:53:02 +08:00
|
|
|
OutMI.setOpcode(MI->getOpcode());
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2010-11-15 03:53:02 +08:00
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
MCOperand MCOp;
|
2017-09-23 02:30:02 +08:00
|
|
|
if (LowerPPCMachineOperandToMCOperand(MI->getOperand(i), MCOp, AP,
|
2019-10-06 22:44:22 +08:00
|
|
|
IsDarwin))
|
2017-09-23 02:30:02 +08:00
|
|
|
OutMI.addOperand(MCOp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool llvm::LowerPPCMachineOperandToMCOperand(const MachineOperand &MO,
|
|
|
|
MCOperand &OutMO, AsmPrinter &AP,
|
2019-10-06 22:44:22 +08:00
|
|
|
bool IsDarwin) {
|
2017-09-23 02:30:02 +08:00
|
|
|
switch (MO.getType()) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown operand type");
|
|
|
|
case MachineOperand::MO_Register:
|
|
|
|
assert(!MO.getSubReg() && "Subregs should be eliminated!");
|
|
|
|
assert(MO.getReg() > PPC::NoRegister &&
|
|
|
|
MO.getReg() < PPC::NUM_TARGET_REGS &&
|
|
|
|
"Invalid register for this target!");
|
|
|
|
OutMO = MCOperand::createReg(MO.getReg());
|
|
|
|
return true;
|
|
|
|
case MachineOperand::MO_Immediate:
|
|
|
|
OutMO = MCOperand::createImm(MO.getImm());
|
|
|
|
return true;
|
|
|
|
case MachineOperand::MO_MachineBasicBlock:
|
|
|
|
OutMO = MCOperand::createExpr(
|
|
|
|
MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), AP.OutContext));
|
|
|
|
return true;
|
|
|
|
case MachineOperand::MO_GlobalAddress:
|
|
|
|
case MachineOperand::MO_ExternalSymbol:
|
2019-10-06 22:44:22 +08:00
|
|
|
OutMO = GetSymbolRef(MO, GetSymbolFromOperand(MO, AP), AP, IsDarwin);
|
2017-09-23 02:30:02 +08:00
|
|
|
return true;
|
|
|
|
case MachineOperand::MO_JumpTableIndex:
|
2019-10-06 22:44:22 +08:00
|
|
|
OutMO = GetSymbolRef(MO, AP.GetJTISymbol(MO.getIndex()), AP, IsDarwin);
|
2017-09-23 02:30:02 +08:00
|
|
|
return true;
|
|
|
|
case MachineOperand::MO_ConstantPoolIndex:
|
2019-10-06 22:44:22 +08:00
|
|
|
OutMO = GetSymbolRef(MO, AP.GetCPISymbol(MO.getIndex()), AP, IsDarwin);
|
2017-09-23 02:30:02 +08:00
|
|
|
return true;
|
|
|
|
case MachineOperand::MO_BlockAddress:
|
|
|
|
OutMO = GetSymbolRef(MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()), AP,
|
2019-10-06 22:44:22 +08:00
|
|
|
IsDarwin);
|
2017-09-23 02:30:02 +08:00
|
|
|
return true;
|
2019-07-27 01:25:27 +08:00
|
|
|
case MachineOperand::MO_MCSymbol:
|
2019-10-06 22:44:22 +08:00
|
|
|
OutMO = GetSymbolRef(MO, MO.getMCSymbol(), AP, IsDarwin);
|
2019-07-27 01:25:27 +08:00
|
|
|
return true;
|
2017-09-23 02:30:02 +08:00
|
|
|
case MachineOperand::MO_RegisterMask:
|
|
|
|
return false;
|
2010-11-15 03:53:02 +08:00
|
|
|
}
|
|
|
|
}
|