2012-02-18 20:03:15 +08:00
|
|
|
//===-- HexagonAsmPrinter.cpp - Print machine instrs to Hexagon assembly --===//
|
2011-12-13 05:14:40 +08: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 a printer that converts from our internal representation
|
|
|
|
// of machine-dependent LLVM code to Hexagon assembly language. This printer is
|
|
|
|
// the output mechanism used by `llc'.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "Hexagon.h"
|
2013-02-21 00:13:27 +08:00
|
|
|
#include "HexagonAsmPrinter.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "HexagonMachineFunctionInfo.h"
|
2013-02-21 00:13:27 +08:00
|
|
|
#include "HexagonSubtarget.h"
|
2014-01-07 19:48:04 +08:00
|
|
|
#include "HexagonTargetMachine.h"
|
2014-11-21 05:56:35 +08:00
|
|
|
#include "MCTargetDesc/HexagonInstPrinter.h"
|
2015-02-20 05:10:50 +08:00
|
|
|
#include "MCTargetDesc/HexagonMCInstrInfo.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2012-04-13 01:55:53 +08:00
|
|
|
#include "llvm/Analysis/ConstantFolding.h"
|
2011-12-13 05:14:40 +08:00
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
2014-01-08 05:19:40 +08:00
|
|
|
#include "llvm/IR/Mangler.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2011-12-13 05:14:40 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2012-04-13 01:55:53 +08:00
|
|
|
#include "llvm/MC/MCContext.h"
|
|
|
|
#include "llvm/MC/MCExpr.h"
|
|
|
|
#include "llvm/MC/MCInst.h"
|
|
|
|
#include "llvm/MC/MCSection.h"
|
|
|
|
#include "llvm/MC/MCStreamer.h"
|
2011-12-13 05:14:40 +08:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2012-04-13 01:55:53 +08:00
|
|
|
#include "llvm/Support/Format.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2012-03-18 02:46:09 +08:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-12-13 05:14:40 +08:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Target/TargetLoweringObjectFile.h"
|
2011-12-13 05:14:40 +08:00
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2011-12-13 05:14:40 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:41:26 +08:00
|
|
|
#define DEBUG_TYPE "asm-printer"
|
|
|
|
|
2011-12-13 05:14:40 +08:00
|
|
|
static cl::opt<bool> AlignCalls(
|
|
|
|
"hexagon-align-calls", cl::Hidden, cl::init(true),
|
|
|
|
cl::desc("Insert falign after call instruction for Hexagon target"));
|
|
|
|
|
2015-01-19 04:29:04 +08:00
|
|
|
HexagonAsmPrinter::HexagonAsmPrinter(TargetMachine &TM,
|
|
|
|
std::unique_ptr<MCStreamer> Streamer)
|
2015-02-03 14:40:22 +08:00
|
|
|
: AsmPrinter(TM, std::move(Streamer)), Subtarget(nullptr) {}
|
2015-01-19 04:29:04 +08:00
|
|
|
|
2012-04-13 01:55:53 +08:00
|
|
|
void HexagonAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
|
|
|
|
raw_ostream &O) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(OpNo);
|
|
|
|
|
2011-12-13 05:14:40 +08:00
|
|
|
switch (MO.getType()) {
|
2012-05-04 05:52:53 +08:00
|
|
|
default: llvm_unreachable ("<unknown operand type>");
|
2012-04-13 01:55:53 +08:00
|
|
|
case MachineOperand::MO_Register:
|
|
|
|
O << HexagonInstPrinter::getRegisterName(MO.getReg());
|
|
|
|
return;
|
2011-12-13 05:14:40 +08:00
|
|
|
case MachineOperand::MO_Immediate:
|
2012-04-13 01:55:53 +08:00
|
|
|
O << MO.getImm();
|
|
|
|
return;
|
2011-12-13 05:14:40 +08:00
|
|
|
case MachineOperand::MO_MachineBasicBlock:
|
|
|
|
O << *MO.getMBB()->getSymbol();
|
|
|
|
return;
|
|
|
|
case MachineOperand::MO_ConstantPoolIndex:
|
|
|
|
O << *GetCPISymbol(MO.getIndex());
|
|
|
|
return;
|
2012-04-13 01:55:53 +08:00
|
|
|
case MachineOperand::MO_GlobalAddress:
|
2011-12-13 05:14:40 +08:00
|
|
|
// Computing the address of a global symbol, not calling it.
|
2013-10-30 01:07:16 +08:00
|
|
|
O << *getSymbol(MO.getGlobal());
|
2011-12-13 05:14:40 +08:00
|
|
|
printOffset(MO.getOffset(), O);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// isBlockOnlyReachableByFallthrough - We need to override this since the
|
|
|
|
// default AsmPrinter does not print labels for any basic block that
|
|
|
|
// is only reachable by a fall through. That works for all cases except
|
|
|
|
// for the case in which the basic block is reachable by a fall through but
|
|
|
|
// through an indirect from a jump table. In this case, the jump table
|
|
|
|
// will contain a label not defined by AsmPrinter.
|
|
|
|
//
|
|
|
|
bool HexagonAsmPrinter::
|
|
|
|
isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
|
|
|
|
if (MBB->hasAddressTaken()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return AsmPrinter::isBlockOnlyReachableByFallthrough(MBB);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// PrintAsmOperand - Print out an operand for an inline asm expression.
|
|
|
|
///
|
|
|
|
bool HexagonAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
|
|
|
|
unsigned AsmVariant,
|
|
|
|
const char *ExtraCode,
|
2012-04-13 01:55:53 +08:00
|
|
|
raw_ostream &OS) {
|
2011-12-13 05:14:40 +08:00
|
|
|
// Does this asm operand have a single letter operand modifier?
|
|
|
|
if (ExtraCode && ExtraCode[0]) {
|
|
|
|
if (ExtraCode[1] != 0) return true; // Unknown modifier.
|
|
|
|
|
|
|
|
switch (ExtraCode[0]) {
|
2012-06-26 21:49:27 +08:00
|
|
|
default:
|
|
|
|
// See if this is a generic print operand
|
|
|
|
return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
|
2011-12-13 05:14:40 +08:00
|
|
|
case 'c': // Don't print "$" before a global var name or constant.
|
|
|
|
// Hexagon never has a prefix.
|
|
|
|
printOperand(MI, OpNo, OS);
|
|
|
|
return false;
|
|
|
|
case 'L': // Write second word of DImode reference.
|
|
|
|
// Verify that this operand has two consecutive registers.
|
|
|
|
if (!MI->getOperand(OpNo).isReg() ||
|
|
|
|
OpNo+1 == MI->getNumOperands() ||
|
|
|
|
!MI->getOperand(OpNo+1).isReg())
|
|
|
|
return true;
|
|
|
|
++OpNo; // Return the high-part.
|
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
// Write 'i' if an integer constant, otherwise nothing. Used to print
|
|
|
|
// addi vs add, etc.
|
|
|
|
if (MI->getOperand(OpNo).isImm())
|
|
|
|
OS << "i";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printOperand(MI, OpNo, OS);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HexagonAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
|
|
|
|
unsigned OpNo, unsigned AsmVariant,
|
|
|
|
const char *ExtraCode,
|
|
|
|
raw_ostream &O) {
|
|
|
|
if (ExtraCode && ExtraCode[0])
|
|
|
|
return true; // Unknown modifier.
|
|
|
|
|
|
|
|
const MachineOperand &Base = MI->getOperand(OpNo);
|
|
|
|
const MachineOperand &Offset = MI->getOperand(OpNo+1);
|
|
|
|
|
|
|
|
if (Base.isReg())
|
|
|
|
printOperand(MI, OpNo, O);
|
|
|
|
else
|
2012-02-07 10:50:20 +08:00
|
|
|
llvm_unreachable("Unimplemented");
|
2011-12-13 05:14:40 +08:00
|
|
|
|
|
|
|
if (Offset.isImm()) {
|
|
|
|
if (Offset.getImm())
|
|
|
|
O << " + #" << Offset.getImm();
|
|
|
|
}
|
|
|
|
else
|
2012-02-07 10:50:20 +08:00
|
|
|
llvm_unreachable("Unimplemented");
|
2011-12-13 05:14:40 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// printMachineInstruction -- Print out a single Hexagon MI in Darwin syntax to
|
|
|
|
/// the current output stream.
|
|
|
|
///
|
|
|
|
void HexagonAsmPrinter::EmitInstruction(const MachineInstr *MI) {
|
2012-05-04 05:52:53 +08:00
|
|
|
if (MI->isBundle()) {
|
2014-12-04 04:23:22 +08:00
|
|
|
std::vector<MachineInstr const *> BundleMIs;
|
2012-05-04 05:52:53 +08:00
|
|
|
|
|
|
|
const MachineBasicBlock *MBB = MI->getParent();
|
|
|
|
MachineBasicBlock::const_instr_iterator MII = MI;
|
|
|
|
++MII;
|
|
|
|
unsigned int IgnoreCount = 0;
|
|
|
|
while (MII != MBB->end() && MII->isInsideBundle()) {
|
|
|
|
const MachineInstr *MInst = MII;
|
|
|
|
if (MInst->getOpcode() == TargetOpcode::DBG_VALUE ||
|
2014-12-04 04:23:22 +08:00
|
|
|
MInst->getOpcode() == TargetOpcode::IMPLICIT_DEF) {
|
|
|
|
IgnoreCount++;
|
|
|
|
++MII;
|
|
|
|
continue;
|
2012-05-04 05:52:53 +08:00
|
|
|
}
|
2014-12-04 04:23:22 +08:00
|
|
|
// BundleMIs.push_back(&*MII);
|
2012-05-04 05:52:53 +08:00
|
|
|
BundleMIs.push_back(MInst);
|
|
|
|
++MII;
|
|
|
|
}
|
|
|
|
unsigned Size = BundleMIs.size();
|
2014-12-04 04:23:22 +08:00
|
|
|
assert((Size + IgnoreCount) == MI->getBundleSize() && "Corrupt Bundle!");
|
2012-05-04 05:52:53 +08:00
|
|
|
for (unsigned Index = 0; Index < Size; Index++) {
|
2015-02-20 05:10:50 +08:00
|
|
|
MCInst MCI;
|
2012-05-04 05:52:53 +08:00
|
|
|
|
|
|
|
HexagonLowerToMC(BundleMIs[Index], MCI, *this);
|
2015-02-20 05:10:50 +08:00
|
|
|
HexagonMCInstrInfo::AppendImplicitOperands(MCI);
|
|
|
|
HexagonMCInstrInfo::setPacketBegin(MCI, Index == 0);
|
|
|
|
HexagonMCInstrInfo::setPacketEnd(MCI, Index == (Size - 1));
|
2015-04-25 03:11:51 +08:00
|
|
|
EmitToStreamer(*OutStreamer, MCI);
|
2012-05-04 05:52:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2015-02-20 05:10:50 +08:00
|
|
|
MCInst MCI;
|
2014-12-04 04:23:22 +08:00
|
|
|
HexagonLowerToMC(MI, MCI, *this);
|
2015-02-20 05:10:50 +08:00
|
|
|
HexagonMCInstrInfo::AppendImplicitOperands(MCI);
|
2012-05-04 05:52:53 +08:00
|
|
|
if (MI->getOpcode() == Hexagon::ENDLOOP0) {
|
2015-02-20 05:10:50 +08:00
|
|
|
HexagonMCInstrInfo::setPacketBegin(MCI, true);
|
|
|
|
HexagonMCInstrInfo::setPacketEnd(MCI, true);
|
2012-05-04 05:52:53 +08:00
|
|
|
}
|
2015-04-25 03:11:51 +08:00
|
|
|
EmitToStreamer(*OutStreamer, MCI);
|
2012-05-04 05:52:53 +08:00
|
|
|
}
|
2011-12-13 05:14:40 +08:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void LLVMInitializeHexagonAsmPrinter() {
|
|
|
|
RegisterAsmPrinter<HexagonAsmPrinter> X(TheHexagonTarget);
|
|
|
|
}
|