2009-10-20 04:21:05 +08:00
|
|
|
//===-- ARMMCInstLower.cpp - Convert ARM 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 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"
|
2014-03-23 07:51:00 +08:00
|
|
|
#include "MCTargetDesc/ARMBaseInfo.h"
|
2011-07-21 07:34:39 +08:00
|
|
|
#include "MCTargetDesc/ARMMCExpr.h"
|
2009-10-20 08:52:47 +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"
|
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"
|
|
|
|
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) {
|
2016-06-20 23:37:15 +08:00
|
|
|
const MCExpr *Expr =
|
|
|
|
MCSymbolRefExpr::create(Symbol, MCSymbolRefExpr::VK_None, OutContext);
|
|
|
|
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 =
|
|
|
|
MCSymbolRefExpr::create(Symbol, MCSymbolRefExpr::VK_None, OutContext);
|
|
|
|
Expr = ARMMCExpr::createLower16(Expr, OutContext);
|
|
|
|
break;
|
|
|
|
case ARMII::MO_HI16:
|
|
|
|
Expr =
|
|
|
|
MCSymbolRefExpr::create(Symbol, MCSymbolRefExpr::VK_None, OutContext);
|
|
|
|
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;
|
2013-11-26 00:24:52 +08:00
|
|
|
case MachineOperand::MO_GlobalAddress: {
|
|
|
|
MCOp = GetSymbolRef(MO,
|
|
|
|
GetARMGVSymbol(MO.getGlobal(), MO.getTargetFlags()));
|
2011-07-09 01:40:42 +08:00
|
|
|
break;
|
2013-11-26 00:24:52 +08:00
|
|
|
}
|
2011-07-09 01:40:42 +08:00
|
|
|
case MachineOperand::MO_ExternalSymbol:
|
|
|
|
MCOp = GetSymbolRef(MO,
|
|
|
|
GetExternalSymbolSymbol(MO.getSymbolName()));
|
|
|
|
break;
|
|
|
|
case MachineOperand::MO_JumpTableIndex:
|
|
|
|
MCOp = GetSymbolRef(MO, GetJTISymbol(MO.getIndex()));
|
|
|
|
break;
|
|
|
|
case MachineOperand::MO_ConstantPoolIndex:
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-10-20 04:21:05 +08:00
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
2010-09-14 02:25:42 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|