2009-10-20 04:21:05 +08:00
|
|
|
//===-- ARMMCInstLower.cpp - Convert ARM 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
|
2009-10-20 04:21:05 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains code to lower ARM MachineInstrs to their corresponding
|
|
|
|
// MCInst records.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-11-15 04:31:06 +08:00
|
|
|
#include "ARM.h"
|
2010-12-01 11:45:07 +08:00
|
|
|
#include "ARMAsmPrinter.h"
|
2017-01-27 07:40:06 +08:00
|
|
|
#include "ARMBaseInstrInfo.h"
|
|
|
|
#include "ARMMachineFunctionInfo.h"
|
|
|
|
#include "ARMSubtarget.h"
|
|
|
|
#include "MCTargetDesc/ARMAddressingModes.h"
|
2014-03-23 07:51:00 +08:00
|
|
|
#include "MCTargetDesc/ARMBaseInfo.h"
|
2011-07-21 07:34:39 +08:00
|
|
|
#include "MCTargetDesc/ARMMCExpr.h"
|
2017-01-27 07:40:06 +08:00
|
|
|
#include "llvm/ADT/APFloat.h"
|
2009-10-20 08:52:47 +08:00
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
2017-01-27 07:40:06 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
|
|
|
#include "llvm/CodeGen/MachineOperand.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/MC/MCContext.h"
|
add jump tables, constant pools and some trivial global
lowering stuff. We can now compile hello world to:
_main:
stm ,
mov r7, sp
sub sp, sp, #4
mov r0, #0
str r0,
ldr r0,
bl _printf
ldr r0,
mov sp, r7
ldm ,
Almost looks like arm code :)
llvm-svn: 84542
2009-10-20 05:53:00 +08:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
2009-10-20 04:21:05 +08:00
|
|
|
#include "llvm/MC/MCInst.h"
|
2016-09-19 08:54:35 +08:00
|
|
|
#include "llvm/MC/MCInstBuilder.h"
|
|
|
|
#include "llvm/MC/MCStreamer.h"
|
2017-01-27 07:40:06 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
2009-10-20 04:21:05 +08:00
|
|
|
|
2017-01-27 07:40:06 +08:00
|
|
|
using namespace llvm;
|
2010-11-15 04:58:38 +08:00
|
|
|
|
2011-07-09 01:40:42 +08:00
|
|
|
MCOperand ARMAsmPrinter::GetSymbolRef(const MachineOperand &MO,
|
|
|
|
const MCSymbol *Symbol) {
|
2017-02-07 21:07:12 +08:00
|
|
|
MCSymbolRefExpr::VariantKind SymbolVariant = MCSymbolRefExpr::VK_None;
|
|
|
|
if (MO.getTargetFlags() & ARMII::MO_SBREL)
|
|
|
|
SymbolVariant = MCSymbolRefExpr::VK_ARM_SBREL;
|
|
|
|
|
2016-06-20 23:37:15 +08:00
|
|
|
const MCExpr *Expr =
|
2017-02-07 21:07:12 +08:00
|
|
|
MCSymbolRefExpr::create(Symbol, SymbolVariant, OutContext);
|
2016-06-20 23:37:15 +08:00
|
|
|
switch (MO.getTargetFlags() & ARMII::MO_OPTION_MASK) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unknown target flag on symbol operand");
|
|
|
|
case ARMII::MO_NO_FLAG:
|
|
|
|
break;
|
|
|
|
case ARMII::MO_LO16:
|
|
|
|
Expr =
|
2017-02-07 21:07:12 +08:00
|
|
|
MCSymbolRefExpr::create(Symbol, SymbolVariant, OutContext);
|
2016-06-20 23:37:15 +08:00
|
|
|
Expr = ARMMCExpr::createLower16(Expr, OutContext);
|
|
|
|
break;
|
|
|
|
case ARMII::MO_HI16:
|
|
|
|
Expr =
|
2017-02-07 21:07:12 +08:00
|
|
|
MCSymbolRefExpr::create(Symbol, SymbolVariant, OutContext);
|
2016-06-20 23:37:15 +08:00
|
|
|
Expr = ARMMCExpr::createUpper16(Expr, OutContext);
|
2010-09-23 07:27:36 +08:00
|
|
|
break;
|
2009-10-20 08:56:16 +08:00
|
|
|
}
|
2010-12-01 07:29:24 +08:00
|
|
|
|
2010-09-18 02:25:25 +08:00
|
|
|
if (!MO.isJTI() && MO.getOffset())
|
2015-05-30 09:25:56 +08:00
|
|
|
Expr = MCBinaryExpr::createAdd(Expr,
|
|
|
|
MCConstantExpr::create(MO.getOffset(),
|
2011-07-09 01:40:42 +08:00
|
|
|
OutContext),
|
|
|
|
OutContext);
|
2015-05-14 02:37:00 +08:00
|
|
|
return MCOperand::createExpr(Expr);
|
2010-12-01 07:29:24 +08:00
|
|
|
|
2010-09-18 02:25:25 +08:00
|
|
|
}
|
|
|
|
|
2011-07-09 01:40:42 +08:00
|
|
|
bool ARMAsmPrinter::lowerOperand(const MachineOperand &MO,
|
|
|
|
MCOperand &MCOp) {
|
|
|
|
switch (MO.getType()) {
|
2012-02-07 10:50:20 +08:00
|
|
|
default: llvm_unreachable("unknown operand type");
|
2011-07-09 01:40:42 +08:00
|
|
|
case MachineOperand::MO_Register:
|
|
|
|
// Ignore all non-CPSR implicit register operands.
|
|
|
|
if (MO.isImplicit() && MO.getReg() != ARM::CPSR)
|
|
|
|
return false;
|
|
|
|
assert(!MO.getSubReg() && "Subregs should be eliminated!");
|
2015-05-14 02:37:00 +08:00
|
|
|
MCOp = MCOperand::createReg(MO.getReg());
|
2011-07-09 01:40:42 +08:00
|
|
|
break;
|
|
|
|
case MachineOperand::MO_Immediate:
|
2015-05-14 02:37:00 +08:00
|
|
|
MCOp = MCOperand::createImm(MO.getImm());
|
2011-07-09 01:40:42 +08:00
|
|
|
break;
|
|
|
|
case MachineOperand::MO_MachineBasicBlock:
|
2015-05-30 09:25:56 +08:00
|
|
|
MCOp = MCOperand::createExpr(MCSymbolRefExpr::create(
|
2011-07-09 01:40:42 +08:00
|
|
|
MO.getMBB()->getSymbol(), OutContext));
|
|
|
|
break;
|
2017-01-27 07:40:06 +08:00
|
|
|
case MachineOperand::MO_GlobalAddress:
|
2013-11-26 00:24:52 +08:00
|
|
|
MCOp = GetSymbolRef(MO,
|
|
|
|
GetARMGVSymbol(MO.getGlobal(), MO.getTargetFlags()));
|
2011-07-09 01:40:42 +08:00
|
|
|
break;
|
|
|
|
case MachineOperand::MO_ExternalSymbol:
|
2016-06-21 23:21:04 +08:00
|
|
|
MCOp = GetSymbolRef(MO,
|
2011-07-09 01:40:42 +08:00
|
|
|
GetExternalSymbolSymbol(MO.getSymbolName()));
|
|
|
|
break;
|
|
|
|
case MachineOperand::MO_JumpTableIndex:
|
|
|
|
MCOp = GetSymbolRef(MO, GetJTISymbol(MO.getIndex()));
|
|
|
|
break;
|
|
|
|
case MachineOperand::MO_ConstantPoolIndex:
|
2016-12-15 15:59:08 +08:00
|
|
|
if (Subtarget->genExecuteOnly())
|
|
|
|
llvm_unreachable("execute-only should not generate constant pools");
|
2011-07-09 01:40:42 +08:00
|
|
|
MCOp = GetSymbolRef(MO, GetCPISymbol(MO.getIndex()));
|
|
|
|
break;
|
|
|
|
case MachineOperand::MO_BlockAddress:
|
|
|
|
MCOp = GetSymbolRef(MO, GetBlockAddressSymbol(MO.getBlockAddress()));
|
|
|
|
break;
|
|
|
|
case MachineOperand::MO_FPImmediate: {
|
|
|
|
APFloat Val = MO.getFPImm()->getValueAPF();
|
|
|
|
bool ignored;
|
2016-12-14 19:57:17 +08:00
|
|
|
Val.convert(APFloat::IEEEdouble(), APFloat::rmTowardZero, &ignored);
|
2015-05-14 02:37:00 +08:00
|
|
|
MCOp = MCOperand::createFPImm(Val.convertToDouble());
|
2011-07-09 01:40:42 +08:00
|
|
|
break;
|
|
|
|
}
|
2012-01-19 07:52:19 +08:00
|
|
|
case MachineOperand::MO_RegisterMask:
|
|
|
|
// Ignore call clobbers.
|
|
|
|
return false;
|
2011-07-09 01:40:42 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-11-15 05:00:02 +08:00
|
|
|
void llvm::LowerARMMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI,
|
2010-12-01 11:45:07 +08:00
|
|
|
ARMAsmPrinter &AP) {
|
2009-10-20 04:21:05 +08:00
|
|
|
OutMI.setOpcode(MI->getOpcode());
|
2010-09-14 02:25:42 +08:00
|
|
|
|
2014-12-02 18:53:20 +08:00
|
|
|
// In the MC layer, we keep modified immediates in their encoded form
|
|
|
|
bool EncodeImms = false;
|
|
|
|
switch (MI->getOpcode()) {
|
|
|
|
default: break;
|
|
|
|
case ARM::MOVi:
|
|
|
|
case ARM::MVNi:
|
|
|
|
case ARM::CMPri:
|
|
|
|
case ARM::CMNri:
|
|
|
|
case ARM::TSTri:
|
|
|
|
case ARM::TEQri:
|
|
|
|
case ARM::MSRi:
|
|
|
|
case ARM::ADCri:
|
|
|
|
case ARM::ADDri:
|
|
|
|
case ARM::ADDSri:
|
|
|
|
case ARM::SBCri:
|
|
|
|
case ARM::SUBri:
|
|
|
|
case ARM::SUBSri:
|
|
|
|
case ARM::ANDri:
|
|
|
|
case ARM::ORRri:
|
|
|
|
case ARM::EORri:
|
|
|
|
case ARM::BICri:
|
|
|
|
case ARM::RSBri:
|
|
|
|
case ARM::RSBSri:
|
|
|
|
case ARM::RSCri:
|
|
|
|
EncodeImms = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-07-17 21:15:26 +08:00
|
|
|
for (const MachineOperand &MO : MI->operands()) {
|
2009-10-20 04:21:05 +08:00
|
|
|
MCOperand MCOp;
|
2014-12-02 18:53:20 +08:00
|
|
|
if (AP.lowerOperand(MO, MCOp)) {
|
|
|
|
if (MCOp.isImm() && EncodeImms) {
|
|
|
|
int32_t Enc = ARM_AM::getSOImmVal(MCOp.getImm());
|
|
|
|
if (Enc != -1)
|
|
|
|
MCOp.setImm(Enc);
|
|
|
|
}
|
2011-07-09 01:40:42 +08:00
|
|
|
OutMI.addOperand(MCOp);
|
2014-12-02 18:53:20 +08:00
|
|
|
}
|
2009-10-20 04:21:05 +08:00
|
|
|
}
|
|
|
|
}
|
2016-09-19 08:54:35 +08:00
|
|
|
|
|
|
|
void ARMAsmPrinter::EmitSled(const MachineInstr &MI, SledKind Kind)
|
|
|
|
{
|
|
|
|
if (MI.getParent()->getParent()->getInfo<ARMFunctionInfo>()
|
|
|
|
->isThumbFunction())
|
|
|
|
{
|
|
|
|
MI.emitError("An attempt to perform XRay instrumentation for a"
|
|
|
|
" Thumb function (not supported). Detected when emitting a sled.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
static const int8_t NoopsInSledCount = 6;
|
|
|
|
// We want to emit the following pattern:
|
|
|
|
//
|
|
|
|
// .Lxray_sled_N:
|
|
|
|
// ALIGN
|
|
|
|
// B #20
|
|
|
|
// ; 6 NOP instructions (24 bytes)
|
|
|
|
// .tmpN
|
|
|
|
//
|
|
|
|
// We need the 24 bytes (6 instructions) because at runtime, we'd be patching
|
|
|
|
// over the full 28 bytes (7 instructions) with the following pattern:
|
|
|
|
//
|
|
|
|
// PUSH{ r0, lr }
|
|
|
|
// MOVW r0, #<lower 16 bits of function ID>
|
|
|
|
// MOVT r0, #<higher 16 bits of function ID>
|
|
|
|
// MOVW ip, #<lower 16 bits of address of __xray_FunctionEntry/Exit>
|
|
|
|
// MOVT ip, #<higher 16 bits of address of __xray_FunctionEntry/Exit>
|
|
|
|
// BLX ip
|
|
|
|
// POP{ r0, lr }
|
|
|
|
//
|
|
|
|
OutStreamer->EmitCodeAlignment(4);
|
|
|
|
auto CurSled = OutContext.createTempSymbol("xray_sled_", true);
|
|
|
|
OutStreamer->EmitLabel(CurSled);
|
|
|
|
auto Target = OutContext.createTempSymbol();
|
|
|
|
|
|
|
|
// Emit "B #20" instruction, which jumps over the next 24 bytes (because
|
|
|
|
// register pc is 8 bytes ahead of the jump instruction by the moment CPU
|
|
|
|
// is executing it).
|
|
|
|
// By analogy to ARMAsmPrinter::emitPseudoExpansionLowering() |case ARM::B|.
|
|
|
|
// It is not clear why |addReg(0)| is needed (the last operand).
|
|
|
|
EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::Bcc).addImm(20)
|
|
|
|
.addImm(ARMCC::AL).addReg(0));
|
|
|
|
|
|
|
|
MCInst Noop;
|
2017-04-22 05:48:41 +08:00
|
|
|
Subtarget->getInstrInfo()->getNoop(Noop);
|
2016-09-19 08:54:35 +08:00
|
|
|
for (int8_t I = 0; I < NoopsInSledCount; I++)
|
|
|
|
OutStreamer->EmitInstruction(Noop, getSubtargetInfo());
|
|
|
|
|
|
|
|
OutStreamer->EmitLabel(Target);
|
|
|
|
recordSled(CurSled, MI, Kind);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ARMAsmPrinter::LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr &MI)
|
|
|
|
{
|
|
|
|
EmitSled(MI, SledKind::FUNCTION_ENTER);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ARMAsmPrinter::LowerPATCHABLE_FUNCTION_EXIT(const MachineInstr &MI)
|
|
|
|
{
|
|
|
|
EmitSled(MI, SledKind::FUNCTION_EXIT);
|
|
|
|
}
|
|
|
|
|
2016-10-18 13:54:15 +08:00
|
|
|
void ARMAsmPrinter::LowerPATCHABLE_TAIL_CALL(const MachineInstr &MI)
|
|
|
|
{
|
|
|
|
EmitSled(MI, SledKind::TAIL_CALL);
|
|
|
|
}
|