2012-02-18 13:03:15 +01:00
|
|
|
//===-- MSP430MCInstLower.cpp - Convert MSP430 MachineInstr to an MCInst --===//
|
2010-10-02 03:08:17 +02:00
|
|
|
//
|
|
|
|
// 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 MSP430 MachineInstrs to their corresponding
|
|
|
|
// MCInst records.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MSP430MCInstLower.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2010-10-02 03:08:17 +02:00
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2014-01-03 20:21:54 +01:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2014-01-07 22:19:40 +01:00
|
|
|
#include "llvm/IR/Mangler.h"
|
2010-10-02 03:08:17 +02:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
|
|
|
#include "llvm/MC/MCContext.h"
|
|
|
|
#include "llvm/MC/MCExpr.h"
|
|
|
|
#include "llvm/MC/MCInst.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-01-03 20:21:54 +01:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2010-10-02 03:08:17 +02:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
MCSymbol *MSP430MCInstLower::
|
|
|
|
GetGlobalAddressSymbol(const MachineOperand &MO) const {
|
|
|
|
switch (MO.getTargetFlags()) {
|
|
|
|
default: llvm_unreachable("Unknown target flag on GV operand");
|
|
|
|
case 0: break;
|
|
|
|
}
|
|
|
|
|
2013-10-29 18:07:16 +01:00
|
|
|
return Printer.getSymbol(MO.getGlobal());
|
2010-10-02 03:08:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
MCSymbol *MSP430MCInstLower::
|
|
|
|
GetExternalSymbolSymbol(const MachineOperand &MO) const {
|
|
|
|
switch (MO.getTargetFlags()) {
|
2012-02-07 03:50:20 +01:00
|
|
|
default: llvm_unreachable("Unknown target flag on GV operand");
|
2010-10-02 03:08:17 +02:00
|
|
|
case 0: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Printer.GetExternalSymbolSymbol(MO.getSymbolName());
|
|
|
|
}
|
|
|
|
|
|
|
|
MCSymbol *MSP430MCInstLower::
|
|
|
|
GetJumpTableSymbol(const MachineOperand &MO) const {
|
2015-07-16 08:11:10 +02:00
|
|
|
const DataLayout &DL = Printer.getDataLayout();
|
2010-10-02 03:08:17 +02:00
|
|
|
SmallString<256> Name;
|
2015-07-16 08:11:10 +02:00
|
|
|
raw_svector_ostream(Name) << DL.getPrivateGlobalPrefix() << "JTI"
|
2010-10-02 03:08:17 +02:00
|
|
|
<< Printer.getFunctionNumber() << '_'
|
|
|
|
<< MO.getIndex();
|
|
|
|
|
|
|
|
switch (MO.getTargetFlags()) {
|
|
|
|
default: llvm_unreachable("Unknown target flag on GV operand");
|
|
|
|
case 0: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a symbol for the name.
|
2015-05-18 20:43:14 +02:00
|
|
|
return Ctx.getOrCreateSymbol(Name);
|
2010-10-02 03:08:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
MCSymbol *MSP430MCInstLower::
|
|
|
|
GetConstantPoolIndexSymbol(const MachineOperand &MO) const {
|
2015-07-16 08:11:10 +02:00
|
|
|
const DataLayout &DL = Printer.getDataLayout();
|
2010-10-02 03:08:17 +02:00
|
|
|
SmallString<256> Name;
|
2015-07-16 08:11:10 +02:00
|
|
|
raw_svector_ostream(Name) << DL.getPrivateGlobalPrefix() << "CPI"
|
2010-10-02 03:08:17 +02:00
|
|
|
<< Printer.getFunctionNumber() << '_'
|
|
|
|
<< MO.getIndex();
|
|
|
|
|
|
|
|
switch (MO.getTargetFlags()) {
|
|
|
|
default: llvm_unreachable("Unknown target flag on GV operand");
|
|
|
|
case 0: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a symbol for the name.
|
2015-05-18 20:43:14 +02:00
|
|
|
return Ctx.getOrCreateSymbol(Name);
|
2010-10-02 03:08:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
MCSymbol *MSP430MCInstLower::
|
|
|
|
GetBlockAddressSymbol(const MachineOperand &MO) const {
|
|
|
|
switch (MO.getTargetFlags()) {
|
2012-02-07 03:50:20 +01:00
|
|
|
default: llvm_unreachable("Unknown target flag on GV operand");
|
2010-10-02 03:08:17 +02:00
|
|
|
case 0: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Printer.GetBlockAddressSymbol(MO.getBlockAddress());
|
|
|
|
}
|
|
|
|
|
|
|
|
MCOperand MSP430MCInstLower::
|
|
|
|
LowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym) const {
|
|
|
|
// FIXME: We would like an efficient form for this, so we don't have to do a
|
|
|
|
// lot of extra uniquing.
|
2015-05-30 03:25:56 +02:00
|
|
|
const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx);
|
2010-10-02 03:08:17 +02:00
|
|
|
|
|
|
|
switch (MO.getTargetFlags()) {
|
|
|
|
default: llvm_unreachable("Unknown target flag on GV operand");
|
|
|
|
case 0: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!MO.isJTI() && MO.getOffset())
|
2015-05-30 03:25:56 +02:00
|
|
|
Expr = MCBinaryExpr::createAdd(Expr,
|
|
|
|
MCConstantExpr::create(MO.getOffset(), Ctx),
|
2010-10-02 03:08:17 +02:00
|
|
|
Ctx);
|
2015-05-13 20:37:00 +02:00
|
|
|
return MCOperand::createExpr(Expr);
|
2010-10-02 03:08:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MSP430MCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
|
|
|
|
OutMI.setOpcode(MI->getOpcode());
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
|
|
|
|
|
|
|
MCOperand MCOp;
|
|
|
|
switch (MO.getType()) {
|
|
|
|
default:
|
|
|
|
MI->dump();
|
2012-02-07 03:50:20 +01:00
|
|
|
llvm_unreachable("unknown operand type");
|
2010-10-02 03:08:17 +02:00
|
|
|
case MachineOperand::MO_Register:
|
|
|
|
// Ignore all implicit register operands.
|
|
|
|
if (MO.isImplicit()) continue;
|
2015-05-13 20:37:00 +02:00
|
|
|
MCOp = MCOperand::createReg(MO.getReg());
|
2010-10-02 03:08:17 +02:00
|
|
|
break;
|
|
|
|
case MachineOperand::MO_Immediate:
|
2015-05-13 20:37:00 +02:00
|
|
|
MCOp = MCOperand::createImm(MO.getImm());
|
2010-10-02 03:08:17 +02:00
|
|
|
break;
|
|
|
|
case MachineOperand::MO_MachineBasicBlock:
|
2015-05-30 03:25:56 +02:00
|
|
|
MCOp = MCOperand::createExpr(MCSymbolRefExpr::create(
|
2010-10-02 03:08:17 +02:00
|
|
|
MO.getMBB()->getSymbol(), Ctx));
|
|
|
|
break;
|
|
|
|
case MachineOperand::MO_GlobalAddress:
|
|
|
|
MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
|
|
|
|
break;
|
|
|
|
case MachineOperand::MO_ExternalSymbol:
|
|
|
|
MCOp = LowerSymbolOperand(MO, GetExternalSymbolSymbol(MO));
|
|
|
|
break;
|
|
|
|
case MachineOperand::MO_JumpTableIndex:
|
|
|
|
MCOp = LowerSymbolOperand(MO, GetJumpTableSymbol(MO));
|
|
|
|
break;
|
|
|
|
case MachineOperand::MO_ConstantPoolIndex:
|
|
|
|
MCOp = LowerSymbolOperand(MO, GetConstantPoolIndexSymbol(MO));
|
|
|
|
break;
|
|
|
|
case MachineOperand::MO_BlockAddress:
|
|
|
|
MCOp = LowerSymbolOperand(MO, GetBlockAddressSymbol(MO));
|
2012-01-19 00:52:19 +01:00
|
|
|
break;
|
|
|
|
case MachineOperand::MO_RegisterMask:
|
|
|
|
continue;
|
2010-10-02 03:08:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
OutMI.addOperand(MCOp);
|
|
|
|
}
|
|
|
|
}
|